selenium加载配置参数,让chrome浏览器不出现‘Chrome正在受到自动软件的控制’的提示语,以及后台静默模式运行,不占用桌面的方法

一:自动化测试的时候,启动浏览器出现‘Chrome正在受到自动软件的控制’,怎么样隐藏,今天学习分享:

在浏览器配置里加个参数,忽略掉这个警告提示语,disable_infobars

option = webdriver.ChromeOptions()

#Chrome V78版本以内

option.add_argument('disable-infobars')

#Chrome 版本高于V78版本

option.add_experimental_option("excludeSwitches", ['enable-automation'])

 

参考代码:

import re

from selenium import webdriver
import time
from selenium.webdriver.common.by import By

# 加启动配置

option = webdriver.ChromeOptions()
option.add_experimental_option('useAutomationExtension', False)
option.add_experimental_option("excludeSwitches", ['enable-automation'])
option.add_argument('--start-maximized')


def startUp():

    driver = webdriver.Chrome(options=option)
    driver.maximize_window()
    driver.get("https://www.baidu.com")
    driver.implicitly_wait(3)
    # driver.find_element(By.CSS_SELECTOR,'input[id="kw"]').send_keys('测试开发')
    # time.sleep(3)
    res = driver.find_element(By.XPATH, '//*[@id="hotsearch-content-wrapper"]').get_attribute('innerText')
    driver.implicitly_wait(3)
    print(res)
    res2 = driver.find_element(By.XPATH, '//*[@id="hotsearch-content-wrapper"]').get_attribute('innerHTML')
    print(res2)
    pattern = 'class="title-content-title">(.*?)</span>'
    result = re.findall(pattern, res2)
    print(result)
    driver.get_screenshot_as_file(r'C:\Users\jingwenshuai\Desktop\code\framework\seleniumWeb\a.png')
    driver.quit()


if __name__ == '__main__':
    startUp()

  

 

 

还有可能是你的Chrome浏览器和驱动不匹配

chrome和驱动对应版本号可以查看链接:https://blog.csdn.net/huilan_same/article/details/51896672

由于笔者的版本是67.0,所以应该下的驱动是V2.40 
下载的地址为:http://npm.taobao.org/mirrors/chromedriver/ 或者http://chromedriver.storage.googleapis.com/index.html 

也可参考这篇:https://blog.csdn.net/weixin_44318830/article/details/103339273 

 

二:启动浏览器的时候不想看的浏览器运行,那就加载浏览器的静默模式,让它在后台偷偷运行。用headless

option = webdriver.ChromeOptions()
option.add_argument('headless')

 

代码如下:

# coding:utf-8 

from selenium import webdriver

# 加启动配置

option = webdriver.ChromeOptions()

option.add_argument('headless')

# 打开chrome浏览器

driver = webdriver.Chrome(chrome_options=option)

driver.get("https://www.baidu.com")

 

三、python selenium 调用Chrome,提示不安全的data和您使用的是不支持命令行标记

我用的是chrome 59.0.3071.109(正式版本) (64 位)后来把chromedriver 版本换成 2.31 问题解决了

 

来源:https://www.cnblogs.com/yaohanbaby/p/8080880.html

posted @ 2018-08-16 09:21  香菜哥哥  阅读(1470)  评论(2编辑  收藏  举报