selenium 登陆小技巧

时间:2023-11-09 18:08:08
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(i)
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官方文档。