Python中的Selenium Webdriver示例

vis*_*rge 10 python selenium webdriver

我用Java编写了一个带有Webdriver的scipt,它工作得很好,下面是样本的代码

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");
   }
     }

}
Run Code Online (Sandbox Code Playgroud)

但是我的要求是用webdriver在python中实现相同的功能,请你告诉我如何通过上面的例子和webdriver二进制文件来完成它以及如何为它做同样的设置

gim*_*mel 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()
Run Code Online (Sandbox Code Playgroud)

一个测试模块,GoogleOneBox,模拟一个有谷歌搜索栏的页面(网址移动了一点).

  • 未来3年,测试模块位于http://code.google.com/p/selenium/source/browse/py/test/selenium/webdriver/common/google_one_box.py (2认同)