import unittest from selenium import webdriver from selenium.webdriver.common.keys import Keys class PythonOrgSearch(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() 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.close() if __name__ == "__main__": unittest.main()
测试结果:
C:\Users\jelena.zhao\AppData\Local\Programs\Python\Python36\python3.exe E:/pythonscripts/study/test1/selenium_study/test1.py
.
----------------------------------------------------------------------
Ran 1 test in 4.484s
OK
Process finished with exit code 0