headless Chrome 学习记录

> 借鉴这个教程:https://www.jianshu.com/p/aec4b1216011

headless Chrome 学习记录

示例

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()

headless Chrome 学习记录

上一篇:Collectors.reducing总结


下一篇:重装系统怎么保存原来的文件