一、背景:在使用selenium的时候,需要用到Google浏览器chrome,其中需要chromedrive这个控件。
因为我的电脑上有正常使用的Google浏览器,且版本最新的版本 119.0.6045.200,且这个版本在不断更新,而chromedrive的更新速度是跟不上的,所以会存在没有chromedrive可用的情况,因此,我的解决办法是下载chrome离线版。
二、下载地址
chrome离线版网址:https://commondatastorage.googleapis.com/chromium-browser-snapshots/index.html
Windows电脑选择Win_x64文件夹。
选择合适的版本号,一般来说,应该选择最新的。
选择chrome-win.zip和chromedriver_win32.zip两个文件夹下载。
将两个文件夹解压后,将chromedriver_win32中的文件chromedriver.exe放到chrome_driver的文件内。
三、使用selenium的代码。
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service=Service(executable_path='C:/Users/理想主义者/Downloads/chrome-win/chromedriver.exe') # 将路径替换为您解压缩的ChromeDriver的实际路径
chrome_options = webdriver.ChromeOptions()
chrome_options.binary_location= 'C:/Users/理想主义者/Downloads/chrome-win/chrome.exe' # 将路径替换为您免安装版Chrome可执行文件的实际路径
browser = webdriver.Chrome(service=service,options=chrome_options)
成功会闪现出一个浏览器的框,如果不成功则报错。
四、会出现的问题
1、直接安装chromedriver,很容易出现与浏览器不符合的情况。
2、使用免安装的chrome,不需要添加环境变量,但是在代码中要写对位置。
3、路径要区别:斜杠\和反斜杠/,为了稳妥起见,最好使用反斜杠/。