selenium加载配置参数,让chrome浏览器不出现‘Chrome正在受到自动软件的控制’的提示语,以及后台静默模式运行,不占用桌面的方法
一:自动化测试的时候,启动浏览器出现‘Chrome正在受到自动软件的控制’,怎么样隐藏,今天学习分享:
在浏览器配置里加个参数,忽略掉这个警告提示语,disable_infobars
option = webdriver.ChromeOptions()
参考代码:
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

浙公网安备 33010602011771号