Chrome Headless模式

做webUI的自动化,当本地执行脚本时,会启动浏览器的UI界面,导致其他工作无法进行。并且当电脑锁屏时,chromer driver 会退出,无法继续执行。
Headerless Browser(无头的浏览器)是浏览器的无界面状态,可以在不打开浏览器GUI的情况下,使用浏览器支持的性能。

Chrome Headless相比于其他的浏览器,可以更便捷的运行web自动化,编写爬虫、截图等。通常是由编程或者命令行来控制的。

好处:

可以加快UI自动化测试的执行时间,对于UI自动化测试,少了真实浏览器加载css,js以及渲染页面的工作。无头测试要比真实浏览器快的多。

可以在无界面的服务器或CI上运行测试,减少了外界的干扰,使自动化测试更稳定。

Python Selenium 用法:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By


def test_headless():
    options = Options()
    options.add_argument('--headless')
    options.add_argument('--window-size=1280,1696')
    driver = webdriver.Chrome(options=options)
    driver.implicitly_wait(5)
    driver.get('https://www.baidu.com/')
    driver.find_element(By.CSS_SELECTOR,'.s_ipt').send_keys('selenium')
    driver.find_element(By.CSS_SELECTOR,'#su').click()

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
#实例化一个启动参数对象
set_options = Options()
#配置启动项
set_options.add_argument('--headless')#设置无界面模式运行浏览器
set_options.add_argument('--start-maximized')#设置启动浏览器时窗口最大化运行
set_options.add_argument('--incognito')#设置无痕模式
set_options.add_argument('--disable-infobars')#设置禁用浏览器正在被自动化程序控制的提示
set_options.add_argument('--window-size=1928,1080')#设置浏览器分辨率窗口大小
#启动浏览器
driver = webdriver.Chrome(options=set_options)  #原来的chrome_options 已被 python建议为使用options
driver.get('http://www.baidu.com')
  • 常用的启动参数:
posted @ 2020-06-14 11:04  捷后愚生  阅读(747)  评论(0编辑  收藏  举报