1. 程式人生 > >selenium webdriver接口

selenium webdriver接口

getting case web sds htm 簡單的 main == 方式

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
#driver.close()退出一個子頁
driver.quit() 退出瀏覽器 

簡單的小栗子。

其中蠻有趣的一種輸入方式

for i in "sdsfa":
    elem.send_keys("pycon")

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class PythonOrgSearch(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_search_in_python_org(self):
        driver = self.driver
        driver.get("http://www.python.org")
        self.assertIn("Python", driver.title)
        elem = driver.find_element_by_name("q")
        elem.send_keys("pycon")
        elem.send_keys(Keys.RETURN)
        assert "No results found." not in driver.page_source


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

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

  

參考自http://selenium-python.readthedocs.io/getting-started.html官方文檔。

selenium webdriver接口