【问题标题】:Selenium Webdriver example in PythonPython 中的 Selenium Webdriver 示例
【发布时间】:2010-05-04 07:43:23
【问题描述】:

我用 Webdriver 用 Ja​​va 编写了一个 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");
   }
     }

}

但我的要求是在 python 中使用 webdriver 实现相同的功能,请告诉我如何使用上面的示例和 webdriver 二进制文件来实现,以及如何进行设置

【问题讨论】:

    标签: python selenium webdriver


    【解决方案1】:

    您是否阅读了python bindings for WebDriver 的说明?

    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,模拟一个有谷歌搜索栏的页面 (网址移动了一点)。

    【讨论】:

    【解决方案2】:
    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')
    

    【讨论】:

      【解决方案3】:

      这是一个插件,但也许能回答你的问题:

      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 documentation

      运行方式:

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

      【讨论】:

        猜你喜欢
        • 2022-11-02
        • 2018-05-22
        • 2022-10-07
        • 1970-01-01
        • 2016-04-19
        • 2017-10-15
        • 2021-06-08
        • 2014-11-15
        • 1970-01-01
        相关资源
        最近更新 更多