chromedriver与chrome版本不匹配解决办法

如果用selenium时间比较长的话,总会碰到chrome版本和chromedriver版本不匹配问题

这个报错相信大家不少见,这个问题就是因为chromedriver现在和chrome大版本保持一致,否则就启动不了,

而chrome又是常常更新,还是自动的。

 做好的脚本,发给客户后,过一段时间客户就会说跑不了,还得更换chromedriver,很是麻烦

有没有啥办法解决这个问题呢

首先过一下人工更换的流程,

1.首先是打开chrome,查看版本(结果查看版本的时候,他又更新了)

可以看到当前版本是89

 2.去chromedriver的淘宝镜像网站去下载对应的版本 http://npm.taobao.org/mirrors/chromedriver/

有时候会出现多个大版本下面的小版本,随便选一个大版本号一样的就行

3. 进去下载压缩包,按照不同系统进行选择下载

 4.替换之前的chromedriver,到此完成更新流程

以上是人工操作流程,对于开发人员来说,应该是很快的,也就几十秒的事情,对于客户来说,却是半小时都不一定能搞定

下面就是说把这个流程自动化,自动完成整个过程

1,首先是第一步,查看chrome版本

这里说一下,我这边都是win平台,其他平台自行解决,思路都是一样的

这里绕了一些弯路

之前思路是先通过where 命令来搜索chrome.exe,然后从exe中提取版本信息

命令是 where /r C:/ chrome.exe

这个命令也能执行,但是么,如果c盘比较大,搜索就很慢,或者装在其他盘,就搜不到

后来网上找了方法,通过注册表来搜索,提供的一个思路是注册表的卸载软件列表,里面又版本信息

路径分32位系统和64位系统

HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionUninstallGoogle Chrome

HKEY_LOCAL_MACHINESOFTWAREWOW6432NodeMicrosoftWindowsCurrentVersionUninstallGoogle Chrome

通过这个路径可以获取到chrome的版本信息

python下面调用winreg

#64位
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,r"SOFTWAREWOW6432NodeMicrosoftWindowsCurrentVersionUninstallGoogle Chrome")
#32位
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,r"SOFTWAREMicrosoftWindowsCurrentVersionUninstallGoogle Chrome")
version = winreg.QueryValueEx(key,"version")[0]

 即可获得版本信息

然后获取chromedriver的信息

我这边chromedriver都是放在项目下的,没有放到path下面

通过这个命令可以获取到版本信息 chromedriver.exe --version

 

然后通过匹配这2个的大版本号来判断是否一致

如果不一致,或者不存在chromedriver,就去镜像站下载

http://npm.taobao.org/mirrors/chromedriver/

先搜索这个网站的版本,然后下载chromedriver,解压缩,就完成了自动更新

完整代码如下

import zipfile
import winreg
import re
import requests

def download(link, file_name):
response = requests.get(link)
file = response.content
with open(file_name, 'wb') as f:
f.write(file)
def unzip(zip_file):
extracting = zipfile.ZipFile(zip_file)
extracting.extractall('.')
extracting.close()
os.remove(zip_file)
def re_all(rule, body):
rule_all = re.findall(re.compile(r'%s' % (rule)), body) if len(rule_all) > 0: return rule_all else: return False def get_chromedriver_list(): '''获取驱动列表''' url = 'http://npm.taobao.org/mirrors/chromedriver/' r = requests.get(url).text return re_all(r'/mirrors/chromedriver/([0-9]+.[0-9]+.[0-9]+.[0-9]+)/', r) def get_chromedriver(version): '''下载驱动''' link = 'http://npm.taobao.org/mirrors/chromedriver/%s/chromedriver_win32.zip' % version if os.path.exists('chromedriver.exe'): os.remove('chromedriver.exe') download(link, 'chromedriver.zip') unzip('chromedriver.zip') return True def get_chrome_version(): key = False if not key: #64位 try: key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SOFTWAREWOW6432NodeMicrosoftWindowsCurrentVersionUninstallGoogle Chrome") except: pass if not key: # 32位 try: key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SOFTWAREMicrosoftWindowsCurrentVersionUninstallGoogle Chrome") except: pass if not key: return False version = winreg.QueryValueEx(key, "version")[0] return version def get_driver_version(): info = os.popen('chromedriver.exe --version').read().strip() if info: return info.split(' ')[1] return False def check_driver_version(): logger.info('获取 chromedriver 版本') if not os.path.exists('chromedriver.exe'): logger.info('没有找到 chromedriver.exe') driver_version = '无' else: driver_version = get_driver_version().split('.')[0] chrome_version = get_chrome_version() if not chrome_version: logger.info('没有按照chrome') return False chrome_version = chrome_version.split('.')[0] if driver_version != chrome_version.split('.')[0]: logger.info('浏览器(%s)和驱动版本(%s)不匹配' % (chrome_version, driver_version)) version_list = get_chromedriver_list() if not version_list: logger.info('获取驱动列表失败') return False for version in version_list: if chrome_version in version: if not get_chromedriver(version): logger.info('下载驱动失败') return False else: logger.info('驱动更新成功') return True logger.info('没有找到对应的驱动版本') return False else: logger.info('版本匹配通过') return True

 然后再启动chrome之前,放入这个检测,就能完成自动更新匹配了

 正好更新,测试下,效果如下,就1秒钟完成了检测,下载,替换,这样就再也不用管这个版本匹配问题了

原文地址:https://www.cnblogs.com/darkspr/p/14548819.html