【python+selenium自动化】设置Chrome启动参数

起因:直接用selenium的webdriver启动chrome,会弹出“Chrome正在受到自动软件的控制”,并且窗口较小,是因为chrome没有加载任何配置

解决:点进selenium的ChromeOptions源码,可见其提供了如下方法

添加启动参数即可,项目中的设置webdrier的代码展示如下

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from apitest.uitest.UIMethod import getyamlconf


class DriverConfig:

    def driver_config(self):
        """
        浏览器驱动
        :return:
        """
        # 实例化ChromeOptions
        options = webdriver.ChromeOptions()
        # 关闭浏览器提示信息
        options.add_argument('disable-infobars')
        # 浏览器全屏
        options.add_argument('start-fullscreen')
        # 设置默认下载目录
        download_path = getyamlconf.GetConf().get_joinpath() + r"RequestsapitestuitestDownloadFile"
        prefs = {'download.default_directory': download_path}
        options.add_experimental_option('prefs', prefs)
        # 获取谷歌浏览器所有控制台信息
        des = DesiredCapabilities.CHROME
        des['loggingPrefs'] = {'performance': 'ALL'}
        # 谷歌浏览器驱动路径
        joinpath = getyamlconf.GetConf().get_joinpath()
        driverpath = joinpath + r'RequestsapitestuitestWebDriverchromedriver.exe'
        # 浏览器驱动
        driver = webdriver.Chrome(driverpath, options=options, desired_capabilities=des)
        # driver = webdriver.Remote(command_executor="http://127.0.0.1:4444/wd/hub", desired_capabilities=des,
        #                           options=options)
        implicitly_wait = getyamlconf.GetConf().get_implicitly_wait()
        driver.implicitly_wait(implicitly_wait)
        return driver

这里我添加了:关闭浏览器提示信息、浏览器全屏、设置默认下载目录(用来处理文件下载后的比对)、控制台信息

原文地址:https://www.cnblogs.com/fengzx120/p/10880014.html