> 借鉴这个教程:https://www.jianshu.com/p/aec4b1216011
示例
import time
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class PythonOrgSearch(unittest.TestCase):
def setUp(self):
self.options = webdriver.ChromeOptions()
self.options.add_argument("--headless") # 设置无头浏览器
self.options.add_argument("--disable-gpu")
self.options.add_argument("--window-size=1280,1696") # 设置窗口大小
self.driver = webdriver.Chrome(executable_path=r"D:\tools\webdrivers\chromedriver92.0.4515.107.exe", options=self.options)
def test_search_in_python_org(self):
driver = self.driver
driver.get("http://www.baidu.com")
driver.save_screenshot(‘1-1.jpg‘)
self.assertIn("百度", driver.title)
elem = driver.find_element_by_name("wd")
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
time.sleep(2)
driver.save_screenshot(‘1-2.jpg‘)
assert "No results found." not in driver.page_source
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()