selenium报Unable to locate or obtain driver for {[‘browserName‘]}
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
# base_path = ((__file__))
base_path = os.getcwd()
def get_driver(url):
# 关闭保存密码提示框,关闭不安全提示
prefs = {"":""}
# 是否启用凭据服务
prefs["credentials_enable_service"] = False
# 是否启用密码管理器配置文件
prefs["profile.password_manager_enabled"] = False
# 启用安全浏览器模式
#prefs[""] = True
prefs[""] = False
# 设置浏览器默认下载目录
# prefs["download.default_ directory"] = r"c:\download"
prefs["download.default_ directory"] = base_path
options = webdriver.ChromeOptions()
# 设置和的目录
options._binary_location = base_path + r'\chrome\112.0.5615.138\chrome\Chrome-bin\'
driver_path = base_path + r"\chrome\112.0.5615.138\chrome\Chrome-bin\"
# 执行完后不自动关闭浏览器
options.add_experimental_option('detach',True)
# 关闭下载保护
options.add_experimental_option("--safebrowsing-disable-download-protection")
options.add_experimental_option("--safebrowsing-disable-extension-blacklist")
# 关闭保存密码提示框,
options.add_experimental_option('prefs', prefs)
# 设置浏览器分辨率
options.add_argument("--window-size=1920,1080")
# 浏览器窗口最大化
options.add_argument('--start-maximized')
# 不显示浏览器
options.add_argument("--headless=new")
# 禁用GPU
options.add_argument('--disable-gpu')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--no-sandbox')# linux only
# 忽略证书错误
options.add_argument('--ignore-certificate-errors')
# 如何去掉提示“正受到自动测试软件控制”
options.add_experimental_option("excludeSwitches", ['enable-automation'])
# 不显示图片
options.add_argument('blink-settings-imagesEnabled=false')
service = Service(executable_path=driver_path)
driver = webdriver.Chrome(service=service,options=options)
driver.get(url)
return driver