之前寫了一個(gè)get_dirver函數(shù),單獨(dú)運(yùn)行的時(shí)候沒問題。后面導(dǎo)入調(diào)用的時(shí)候也是正常的。
但是后面把這個(gè)合入到另一個(gè)項(xiàng)目的時(shí)候就報(bào)Unable to locate or obtain driver for {options.capabilities[‘browserName’]}這個(gè)錯(cuò)誤。
后面編輯源文件,print了一下函數(shù)里面獲取的路徑,顯示不是在當(dāng)前目錄,也就是base_path = os.getcwd()獲取到的不是當(dāng)前目錄。
后來我查了一下,獲取當(dāng)前目錄不能用base_path = os.getcwd(),而要使用文章來源:http://www.zghlxwxcb.cn/news/detail-761248.html
base_path = os.path.dirname(os.path.abspath(__file__))
先獲取本文件的絕對(duì)目錄,然后再截取當(dāng)前目錄文章來源地址http://www.zghlxwxcb.cn/news/detail-761248.html
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
# base_path = os.path.dirname(os.path.abspath(__file__))
base_path = os.getcwd()
def get_driver(url):
# 關(guān)閉保存密碼提示框,關(guān)閉不安全提示
prefs = {"":""}
# 是否啟用憑據(jù)服務(wù)
prefs["credentials_enable_service"] = False
# 是否啟用密碼管理器配置文件
prefs["profile.password_manager_enabled"] = False
# 啟用安全瀏覽器模式
#prefs["safebrowsing.enabled"] = True
prefs["safebrowsing.enabled"] = False
# 設(shè)置瀏覽器默認(rèn)下載目錄
# prefs["download.default_ directory"] = r"c:\download"
prefs["download.default_ directory"] = base_path
options = webdriver.ChromeOptions()
# 設(shè)置chrome.exe和chromedriver.exe的目錄
options._binary_location = base_path + r'\chrome\112.0.5615.138\chrome\Chrome-bin\chrome.exe'
driver_path = base_path + r"\chrome\112.0.5615.138\chrome\Chrome-bin\chromedriver.exe"
# 執(zhí)行完后不自動(dòng)關(guān)閉瀏覽器
options.add_experimental_option('detach',True)
# 關(guān)閉下載保護(hù)
options.add_experimental_option("--safebrowsing-disable-download-protection")
options.add_experimental_option("--safebrowsing-disable-extension-blacklist")
# 關(guān)閉保存密碼提示框,
options.add_experimental_option('prefs', prefs)
# 設(shè)置瀏覽器分辨率
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
# 忽略證書錯(cuò)誤
options.add_argument('--ignore-certificate-errors')
# 如何去掉提示“正受到自動(dòng)測(cè)試軟件控制”
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
到了這里,關(guān)于selenium報(bào)Unable to locate or obtain driver for {options.capabilities[‘browserName‘]}的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!