zoukankan      html  css  js  c++  java
  • 【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

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

  • 相关阅读:
    【学习笔记 2】单调队列 & 单调栈
    【学习笔记 1】快速幂
    题解P1151
    题解 P6161【[Cnoi2020]高维】
    不知道叫啥的题目1
    神秘题目1
    5.30 模拟赛赛后总结
    矩阵乘法加速图上问题专题总结
    点分治&点分树 复习
    5.26赛后总结
  • 原文地址:https://www.cnblogs.com/fengzx120/p/10880014.html
Copyright © 2011-2022 走看看