Python中的Selenium Webdriver示例

10 投票
3 回答
26989 浏览
提问于 2025-04-15 22:19

我写了一个用Java和Webdriver的脚本,它运行得很好,下面是示例代码

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.thoughtworks.selenium.Selenium;
import java.util.*;
import java.lang.Thread.*;

public class Login {

 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
 }

 @AfterClass
 public static void tearDownAfterClass() throws Exception {
 }

 @Before
 public void setUp() throws Exception {
 }

 @After
 public void tearDown() throws Exception {
 }

    public static void main(String[] args) {
         WebDriver driver = new FirefoxDriver();
         Selenium selenium = new WebDriverBackedSelenium(driver,     "http://192.168.10.10:8080/");
         selenium.open("/");
   selenium.keyPress("name=user_id", "admin");
   }
     }

}

但是我现在需要用Python和Webdriver来实现同样的功能,你能告诉我怎么用上面的例子和Webdriver的程序来做到这一点吗?还有怎么设置这些东西?

3 个回答

1

这是一个推广,但可能对你的问题有一点帮助:

import unittest
from holmium.core import PageObject, PageElement, PageElements, Locators

class GoogleMain(PageObject):
    search_box = PageElement( Locators.NAME, "q", timeout = 1)
    search_results = PageElements( Locators.CSS_SELECTOR, "li.g", timeout = 1)

    def search ( self, query ):
        self.search_box.clear()
        self.search_box.send_keys(query)
        self.search_box.submit()

class Test(unittest.TestCase):
    def test_search_simple(self):
        self.assertTrue(
                len( GoogleMain(self.driver, "http://google.com").search( "selenium" ).search_results) > 0
        )

详细信息可以在 holmium.core 的文档中找到,点击这里查看 holmium.core 文档

运行方式如下:

nosetests test_google.py --with-holmium --holmium-browser=firefox 
2

在编程中,有时候我们会遇到一些问题,特别是在使用某些工具或框架的时候。这些问题可能会让我们感到困惑,不知道该怎么解决。比如,有人可能会在使用某个库时,发现它的某个功能没有按预期工作,或者出现了错误信息。这时候,我们可以去网上的技术论坛,比如StackOverflow,寻求帮助。

在这些论坛上,很多人会分享他们的经验和解决方案。你可以看到其他人遇到的类似问题,以及他们是如何解决的。这不仅能帮助你找到答案,还能让你学到更多的知识,了解这个工具或框架的使用方法。

总之,遇到问题时,不要害怕去寻求帮助,网上有很多资源可以让你更好地理解和解决问题。

import unittest
from selenium import webdriver

class Login(unittest.TestCase):
    def setUp(self):
       self.driver = webdriver.Firefox()

    def tearDown(self):
       self.driver.quit()

    def test_login(self):
       driver = self.driver
       driver.get('http://testurl')
       username = driver.find_element_by_name('user_id')
       username.send_keys('admin')
12

你有没有看过这个链接里的说明:WebDriver的Python绑定

example2.py 这个例子很清楚,虽然它和你的代码不是完全一样:

import unittest
from google_one_box import GoogleOneBox
from selenium.firefox.webdriver import WebDriver

class ExampleTest2(unittest.TestCase):
    """This example shows how to use the page object pattern.

    For more information about this pattern, see:
    http://code.google.com/p/webdriver/wiki/PageObjects
    """

    def setUp(self):
        self._driver = WebDriver()

    def tearDown(self):
        self._driver.quit()

    def testSearch(self):
        google = GoogleOneBox(self._driver, "http://www.google.com")
        res = google.search_for("cheese")
        self.assertTrue(res.link_contains_match_for("Wikipedia"))

if __name__ == "__main__":
    unittest.main()

一个测试模块,GoogleOneBox,模拟了一个有谷歌搜索框的页面(网址稍微变动了一下)。

撰写回答