下面整理了一些使用selenium执行自动化操作时,chrome浏览器的常用的参数配置
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
option = webdriver.ChromeOptions()
option.add_argument('--incognito') # 设置浏览器无痕浏览
option.add_argument('headless') # 浏览器不会显示界面,程序在后台运行
option.add_argument('--ignore-certificate-errors') # 提示‘您的链接不是私密连接,攻击者...’时使用该参数,可忽略证书错误
option.add_argument("--disable-features=EnableNavigationPredictor") # http有时会自动跳转为https,使用该参数可禁止重定向转为https
option.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2}) # 不加载网页图片,可提高网页速度
option.add_experimental_option("detach", True) # 程序结束后浏览器不会关闭
option.add_experimental_option("excludeSwitches", ['enable-automation']) # 屏蔽webdriver特征,去掉谷歌正在受到测试软件的监控
option.add_experimental_option('excludeSwitches', ['enable-logging']) # 若出现警告(Failed to read DnsConfig.),可使用此参数
# 指定浏览器及驱动路径
option.binary_location = r'./chrome-win64/chrome.exe'
driver_path = r"./chromedriver-win64/chromedriver.exe"
driver = webdriver.Chrome(service=Service(driver_path), options=option)
driver.maximize_window() # 浏览器界面最大化
driver.implicitly_wait(60) # 隐式等待60s
driver.get("https://www.baidu")