关于selenium, 你还在因为chromedriver的版本与Chrome的版本不一致,需要手动更新chromedriver而烦恼吗?

这篇具有很好参考价值的文章主要介绍了关于selenium, 你还在因为chromedriver的版本与Chrome的版本不一致,需要手动更新chromedriver而烦恼吗?。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

前言

  • 平时做爬虫我比较喜欢用selenium chrome ,一直困扰我一个问题,就是只要谷歌浏览器更新了,就要重新去下载对应版本的chromedriver_win32,这让我十分烦恼

  • 比如我的谷歌浏览器已经94版本了,但是 chromedriver_win32还停留在92版本,就会报出下面的错误

  • selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 92
    Current browser version is 94.0.4606.71 with binary path C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

  • 下面就解决这个痛点,实现自动下载和本地谷歌浏览器匹配的chromedriver_win32

seleniumchromedriver,chrome版本,python,chrome,selenium,python,1024程序员节

seleniumchromedriver,chrome版本,python,chrome,selenium,python,1024程序员节


selenium基本代码,抛出问题

① 我们新建一个selenium_test.py,下面代码是打开百度页面的基本selenium代码,


from selenium import webdriver
import time

class MySelenium(object):
    def __init__(self):
        self.basic_url = "https://www.baidu.com/"
        self.executable_path = r"./chromedriver_win32/chromedriver.exe"
    @property
    def start_driver(self):
        self.browser = webdriver.Chrome(executable_path=self.executable_path)
        self.browser.maximize_window()
    def request_url(self, url):
        """
        :param url:
        """
        self.browser.get(url)
if __name__ == '__main__':
    start_time = time.time()
    ms = MySelenium()
    ms.start_driver
    ms.request_url(ms.basic_url)
    #ms.close()
    test_time = time.strftime("%H:%M:%S", time.gmtime(time.time() - start_time))
    print(test_time)

② 因为这个代码是我在很早之前写的,当时本地的谷歌浏览器版本还是92,现在已经升级到94了,于是就报了下面的错误,就是版本不匹配

selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 92
Current browser version is 94.0.4606.71 with binary path C:\Program Files (x86)\Google\Chrome\Application\chrome.exe


实现自动下载匹配的chromedriver ,解决问题

问题1,怎么获取本地谷歌浏览器的版本?

①,一般获取本地应用的版本信息,用的都是win32com库的接口,新建文件 download_driver.py具体代码如下

  • 请先知道谷歌浏览器在本地的安装位置,代码中列出来一般情况,特殊安装位置要加进列表local_chrome_paths
from win32com.client import Dispatch
class auto_download_chromedrive(object):
    def __init__(self):
        self.local_chrome_paths = [r"C:\Program Files\Google\Chrome\Application\chrome.exe",
                                   r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"]
    def get_version_via_com(self, filename):
        parser = Dispatch("Scripting.FileSystemObject")
        try:
            version = parser.GetFileVersion(filename)
        except Exception:
            return None
        return version
    def start(self):
        version = list(filter(None, [self.get_version_via_com(p) for p in self.local_chrome_paths]))[0]
        if not version:
            print("check chrome browser version failed!")
            return None
        print("chrome browser version:", version)

if __name__ == "__main__":
    chrome = auto_download_chromedrive()
    chrome.start()

②,输出结果可以得到本地浏览器的版本是94版本的。
seleniumchromedriver,chrome版本,python,chrome,selenium,python,1024程序员节


问题2,怎么知道本地的 chromedriver_win32版本不匹配?

①,我们再看下chrome不匹配的报错,具体的报错位SessionNotCreatedException我们修改下selenium的脚本,加上
try except捕捉这个错误

seleniumchromedriver,chrome版本,python,chrome,selenium,python,1024程序员节

  • 更改下 selenium_test.py文件的脚本, 导入SessionNotCreatedExceptiontry,捕捉到这个报错 return 0
from selenium import webdriver
from selenium.common.exceptions import SessionNotCreatedException    #导入SessionNotCreatedException    
import time

class MySelenium(object):
    def __init__(self):
        self.basic_url = "https://www.baidu.com/"
        self.executable_path = r"./chromedriver_win32/chromedriver.exe"

    @property
    def start_driver(self):
        try:
            self.browser = webdriver.Chrome(executable_path=self.executable_path)
            self.browser.maximize_window()
        except SessionNotCreatedException:
            print("Chrome version unmatch. ")
            return 0
        return 1

问题3,怎么下载最新的chromedriver.exe?

①,首先肯定不能用selenium了,这里我选择了requestslxml 库完成下载任务

如下图 chromedriver.exe 下载地址 和页面结构
seleniumchromedriver,chrome版本,python,chrome,selenium,python,1024程序员节
②,更新download_driver.py,添加一个函数 get_chromedriver_urls,用来解析页面

from win32com.client import Dispatch
import requests
from lxml import etree
class auto_download_chromedrive(object):
    def __init__(self):
        self.chromedrive_url = "https://chromedriver.chromium.org/downloads"
        self.local_chrome_paths = [r"C:\Program Files\Google\Chrome\Application\chrome.exe",
                                   r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"]

        self.headers = {'content-type': 'application/json',
                        'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0'}
    def get_version_via_com(self, filename):
        parser = Dispatch("Scripting.FileSystemObject")
        try:
            version = parser.GetFileVersion(filename)
        except Exception:
            return None
        return version
    def get_chromedriver_urls(self):
        try:
            r = requests.Session()
            response = r.get(self.chromedrive_url, headers=self.headers)
            print(response.status_code, response.encoding)
            html = etree.HTML(response.text, etree.HTMLParser())  # 解析HTML文本内容
            version_href = html.xpath(".//strong//..//@href")
            return version_href
        except Exception:
            return None

    def start(self):
        '''读取本地chrome version'''
        version = list(filter(None, [self.get_version_via_com(p) for p in self.local_chrome_paths]))[0]
        if not version:
            print("check chrome browser version failed!")
            return None
        print("chrome browser version:", version)
        '''下载网页端与本地匹配的chromedriver.exe'''
        version_href = self.get_chromedriver_urls()
        if not version_href:
            print("request %s failed!"%self.chromedrive_url)
            return None
        print("all chrome browser versions can be choosed:\n{}".format(version_href))

if __name__ == "__main__":
    chrome = auto_download_chromedrive()
    chrome.start()

③,输出结果如下图,得到所有的chromedriver的下载地址

seleniumchromedriver,chrome版本,python,chrome,selenium,python,1024程序员节

休息下1分钟。。。
seleniumchromedriver,chrome版本,python,chrome,selenium,python,1024程序员节
④,更新download_driver.py,目的是在所有的版本中找到本地浏览器匹配的版本,并下载下来

  • 新加函数 find_local_version,在所有的版本中找到本地浏览器匹配的版本
  • 然后删减和拼接下url,组成新的url,直指 chromedriver_win32.zip
  • 新加函数 download_chromadrive,根据上面的url,下载指定版本
from win32com.client import Dispatch
import re
import requests
from lxml import etree
class auto_download_chromedrive(object):
    def __init__(self):
        self.chromedrive_url = "https://chromedriver.chromium.org/downloads"
        self.local_chrome_paths = [r"C:\Program Files\Google\Chrome\Application\chrome.exe",
                                   r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"]

        self.headers = {'content-type': 'application/json',
                        'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0'}
    def get_version_via_com(self, filename):
        parser = Dispatch("Scripting.FileSystemObject")
        try:
            version = parser.GetFileVersion(filename)
        except Exception:
            return None
        return version
    def get_chromedriver_urls(self):
        try:
            r = requests.Session()
            response = r.get(self.chromedrive_url, headers=self.headers)
            print(response.status_code, response.encoding)
            html = etree.HTML(response.text, etree.HTMLParser())  # 解析HTML文本内容
            version_href = html.xpath(".//strong//..//@href")
            print("all chrome browser versions can be choosed:")
            for href in version_href:
                print(href)

            return version_href
        except Exception:
            return None
    def download_chromadrive(self, url):
        try:
            r = requests.Session()
            response = r.get(url, headers=self.headers)
            if response.status_code == 200:
                with open("chromedriver_win32.zip", "wb") as f:
                    f.write(response.content)
                    print("下载完成")
                    return 1
            else:
                print('Url请求返回错误,错误码为: %d' % response.status_code)
                return None
        except Exception:
            print("request download chromedriver_win32.zip failed!")
            return None
    def find_local_version(self, loc_ver, all_ver):
        """
        :param loc_ver: 本地浏览器的版本
        :param all_ver: 下载的所有版本浏览器版本
        :return: 找到匹配的,return url,否则return None
        """
        for href in all_ver:
            try:
                res = re.search(r"path=(.*?)/", href)
                find_ver = res.group(1).split(".")[0] #截取大版本
                if loc_ver == find_ver:
                    return href
            except Exception:
                continue

        print("not find match chrome browser{} version!".format(loc_ver))
        return None

    def start(self):
        '''读取本地chrome version'''
        version = list(filter(None, [self.get_version_via_com(p) for p in self.local_chrome_paths]))[0]
        if not version:
            print("check chrome browser version failed!")
            return None
        print("chrome browser version:", version)
        '''下载网页端与本地匹配的chromedriver.exe'''
        version_href = self.get_chromedriver_urls()
        if not version_href:
            print("request %s failed!"%self.chromedrive_url)
            return None
		'''找到'''
        find_url = self.find_local_version(version.split(".")[0], version_href)
        print("找到匹配的版本:\n%s"%find_url)
        if not find_url:
            return None
        version_num = re.search(r"path=(.*?)/", find_url).group(1)
        find_url_2 = find_url.rsplit('/', 2)[0]
        new_url = "{}/{}/chromedriver_win32.zip".format(find_url_2, version_num)
        print("downloading......\n%s"%new_url)
        ret = self.download_chromadrive(new_url)
        if not ret:
            return None

if __name__ == "__main__":
    chrome = auto_download_chromedrive()
    chrome.start()

⑤,输出结果如下图
seleniumchromedriver,chrome版本,python,chrome,selenium,python,1024程序员节

问题4,下载的怎么解压并换掉旧版`?

①,直接删除是肯定不行的,因为,当你使用过selenium之后,必须要在进程找那个杀掉chromedriver.exe才可以替换掉旧版本的。

seleniumchromedriver,chrome版本,python,chrome,selenium,python,1024程序员节

  • 定义一个新函数 kill_process,如果进程中存在chromedriver.exe就杀掉
    def kill_process(self, process_name):
        print("检测{}进程是否存在,存在则杀掉。".format(process_name))
        pl = psutil.pids()
        for pid in pl:
            if psutil.Process(pid).name() == process_name:
                print('{} 存在进程中,杀掉'.format(process_name))
                os.popen('taskkill /f /im %s' %process_name)
                return pid
        print('{} 不存在进程中。'.format(process_name))
        return None
  • 下面一段代码是清除chromedriver_win32文件夹内文件的只读属性(因为我的代码从托管平台同步后就只读了)
        old_driver_path = os.path.join(os.getcwd(), "chromedriver_win32")
        if os.path.exists(old_driver_path):
            for sub_file in os.listdir(old_driver_path):
                os.chmod(os.path.join(old_driver_path, sub_file), stat.S_IRWXU)
        time.sleep(1) #这个delay必须要有,os操作还是需要时间的
  • 下面一段代码解压新版本的chromedriver,替换掉旧版本
        print('''解压 chromedriver_win32.zip,覆盖旧版本''')
        zFile = zipfile.ZipFile(os.path.join(os.getcwd(), "chromedriver_win32.zip"), "r")
        for fileM in zFile.namelist():
            zFile.extract(fileM, old_driver_path)
        zFile.close()
  • download_driver.py 完整代码如下:
from win32com.client import Dispatch
import re
import stat,zipfile,os,psutil
import requests
from lxml import etree
import time

class auto_download_chromedrive(object):
    def __init__(self):
        self.chromedrive_url = "https://chromedriver.chromium.org/downloads"
        self.local_chrome_paths = [r"C:\Program Files\Google\Chrome\Application\chrome.exe",
                                   r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"]

        self.headers = {'content-type': 'application/json',
                        'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0'}
    def get_version_via_com(self, filename):
        parser = Dispatch("Scripting.FileSystemObject")
        try:
            version = parser.GetFileVersion(filename)
        except Exception:
            return None
        return version
    def get_chromedriver_urls(self):
        try:
            r = requests.Session()
            response = r.get(self.chromedrive_url, headers=self.headers)
            print(response.status_code, response.encoding)
            html = etree.HTML(response.text, etree.HTMLParser())  # 解析HTML文本内容
            version_href = html.xpath(".//strong//..//@href")
            print("all chrome browser versions can be choosed:")
            for href in version_href:
                print(href)

            return version_href
        except Exception:
            return None
    def download_chromadrive(self, url):
        try:
            r = requests.Session()
            response = r.get(url, headers=self.headers)
            if response.status_code == 200:
                with open("chromedriver_win32.zip", "wb") as f:
                    f.write(response.content)
                    print("下载完成")
                    return 1
            else:
                print('Url请求返回错误,错误码为: %d' % response.status_code)
                return None
        except Exception:
            print("request download chromedriver_win32.zip failed!")
            return None
    def find_local_version(self, loc_ver, all_ver):
        """
        :param loc_ver: 本地浏览器的版本
        :param all_ver: 下载的所有版本浏览器版本
        :return: 找到匹配的,return url,否则return None
        """
        for href in all_ver:
            try:
                res = re.search(r"path=(.*?)/", href)
                find_ver = res.group(1).split(".")[0] #截取大版本
                if loc_ver == find_ver:
                    return href
            except Exception:
                continue

        print("not find match chrome browser{} version!".format(loc_ver))
        return None
    def kill_process(self, process_name):
        print("检测{}进程是否存在,存在则杀掉。".format(process_name))
        pl = psutil.pids()
        for pid in pl:
            if psutil.Process(pid).name() == process_name:
                print('{} 存在进程中,杀掉'.format(process_name))
                os.popen('taskkill /f /im %s' %process_name)
                return pid
        print('{} 不存在进程中。'.format(process_name))
        return None

    def unzip(self):
        self.kill_process("chromedriver.exe")
        print("去除旧版本chromedriver_win32文件夹内文件的只读属性(如果是只读)")
        old_driver_path = os.path.join(os.getcwd(), "chromedriver_win32")
        if os.path.exists(old_driver_path):
            for sub_file in os.listdir(old_driver_path):
                os.chmod(os.path.join(old_driver_path, sub_file), stat.S_IRWXU)
        time.sleep(1) #这个delay必须要有,os操作还是需要时间的
        print('''解压 chromedriver_win32.zip,覆盖旧版本''')
        zFile = zipfile.ZipFile(os.path.join(os.getcwd(), "chromedriver_win32.zip"), "r")
        for fileM in zFile.namelist():
            zFile.extract(fileM, old_driver_path)
        zFile.close()

    def start(self):
        '''读取本地chrome version'''
        version = list(filter(None, [self.get_version_via_com(p) for p in self.local_chrome_paths]))[0]
        if not version:
            print("check chrome browser version failed!")
            return None
        print("chrome browser version:", version)
        '''下载网页端与本地匹配的chromedriver.exe'''
        version_href = self.get_chromedriver_urls()
        if not version_href:
            print("request %s failed!"%self.chromedrive_url)
            return None

        find_url = self.find_local_version(version.split(".")[0], version_href)
        print("找到匹配的版本:\n%s"%find_url)
        if not find_url:
            return None
        version_num = re.search(r"path=(.*?)/", find_url).group(1)
        find_url_2 = find_url.rsplit('/', 2)[0]
        new_url = "{}/{}/chromedriver_win32.zip".format(find_url_2, version_num)
        print("downloading......\n%s"%new_url)
        ret = self.download_chromadrive(new_url)
        if not ret:
            return None
        self.unzip()
if __name__ == "__main__":
    chrome = auto_download_chromedrive()
    chrome.start()

真舒服。。。
seleniumchromedriver,chrome版本,python,chrome,selenium,python,1024程序员节

完善selenium主代码

我们只需要在selenium_test.py中引用download_driver.py中的auto_download_chromedrive类即可
源码我放在git上,如需自取


from selenium import webdriver
from selenium.common.exceptions import SessionNotCreatedException    #导入NoSuchElementException
import time
from download_driver import auto_download_chromedrive

class MySelenium(object):
    def __init__(self):
        self.basic_url = "https://www.baidu.com/"
        self.executable_path = r"./chromedriver_win32/chromedriver.exe"

    @property
    def start_driver(self):
        try:
            self.browser = webdriver.Chrome(executable_path=self.executable_path)
            self.browser.maximize_window()
        except SessionNotCreatedException:
            print("Chrome version unmatch. ")
            return None
        return 1

    def request_url(self, url):
        self.browser.get(url)

if __name__ == '__main__':
    start_time = time.time()
    ms = MySelenium()
    if not ms.start_driver:
        chrome = auto_download_chromedrive()
        chrome.start()
        ms.start_driver
        ms.request_url(ms.basic_url)
        #ms.close()
    test_time = time.strftime("%H:%M:%S", time.gmtime(time.time() - start_time))
    print(test_time)

seleniumchromedriver,chrome版本,python,chrome,selenium,python,1024程序员节

源码我放在git上,如需自取

总结

seleniumchromedriver,chrome版本,python,chrome,selenium,python,1024程序员节

  • 本博客谢绝转载

seleniumchromedriver,chrome版本,python,chrome,selenium,python,1024程序员节文章来源地址https://www.toymoban.com/news/detail-658444.html

  • 要有最朴素的生活,最遥远的梦想,即使明天天寒地冻,路遥马亡!
  • 如果这篇博客对你有帮助,请 “点赞” “评论”“收藏”一键三连 哦!码字不易,大家的支持就是我坚持下去的动力。当然执意选择白嫖也常来哈。
    seleniumchromedriver,chrome版本,python,chrome,selenium,python,1024程序员节

到了这里,关于关于selenium, 你还在因为chromedriver的版本与Chrome的版本不一致,需要手动更新chromedriver而烦恼吗?的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 使用selenium,但chrome无法启动,需要安装浏览器对应版本(最新)的chromedriver

    使用selenium无法启动chrome,代码 报错如下: 安装地址 查询了浏览器版本:117.0.5927 但是国内的镜像网找不到,只更新到114版本 需要安装驱动,点击了最下面的网址 https://chromedriver.chromium.org/home 如图为有效信息 - 点击跳转: https://googlechromelabs.github.io/chrome-for-testing (点击这里

    2024年02月11日
    浏览(50)
  • Python之selenium关于Chrome驱动位置,闪退的问题和安装路径_chromedriver放在哪个目录下

    放置的位置和Python执行的文件位置要一样,这样才能够使用最新的驱动。 注意:在selenium v4.4.0以上的版本,可以不用把浏览器的驱动放在跟Python执行程序一起。 但是需要用一段代码去说明,调用函数。 2、浏览器的驱动与Python启动程序不在一个目录: 方法一:(注意这种目

    2024年04月25日
    浏览(34)
  • chrome浏览器版本和Chromedriver不匹配问题解决办法selenium.common.exceptions.SessionNotCreatedException

    执行selenium抓取的时候,报下面错误: 这是因为浏览器的版本和Chromedriver的版本不匹配,Chrome浏览器如果没有关闭自动更新,会一直出现这个问题,比较麻烦,建议关闭Chrome浏览器自动更新,参照另外一篇文章:Chrome浏览器关闭自动更新 谷歌镜像版本下载链接:https://regist

    2024年02月16日
    浏览(38)
  • Selenium根据Chrome浏览器 版本自动下载/更新驱动chromedriver.exe webdriver_manager库

    Selenium提供了一个webdriver_manager库,可以帮助自动下载和更新Chrome浏览器的驱动程序chromedriver.exe。您可以按照以下步骤操作: 安装webdriver_manager库。可以在命令行或终端中运行以下命令进行安装: 在Selenium Python脚本中,导入webdriver_manager并使用ChromeDriverManager类来创建ChromeDri

    2024年02月16日
    浏览(36)
  • 安装selenium和关于chrome高版本对应的driver驱动下载安装【Win/Mac 】

    目录 一、查看自己电脑上chrome的版本 二、下载 ChromeDriver  三、安装selenium 法一:打开pycharm,点击File,Setting进入配置页面,点击Project下面的Python Interpreter进入环境配置页面,点击+。输入selenium。之后install 四、环境配置 五、验证安装 一、查看自己电脑上chrome的版本 二、下

    2024年04月11日
    浏览(38)
  • Mac系统搭建selenium环境报:无法打开“chromedriver”,因为无法验证开发者 解决办法

    1.安装selenium 打开terminal,使用以下命令安装selenium: pip install -U selenium 2.下载Chromedriver chromedriver 应与chrome版本匹配!!! 在Chrome中输入:chrome://version/ 查看Chrome的版本号信息 然后去地址: https://registry.npmmirror.com/binary.html?path=chromedriver/ 找到和自己Chrome浏览器版本匹配的Chrome dri

    2024年02月06日
    浏览(56)
  • linux下安装 Chrome 和 chromedriver 以及 selenium webdriver 使用

    chromedriver 下载地址: https://googlechromelabs.github.io/chrome-for-testing/ (推荐,包含最新稳定版) https://chromedriver.storage.googleapis.com/index.html? http://npm.taobao.org/mirrors/chromedriver/ https://registry.npmmirror.com/-/binary/chromedriver/ 查看版本: chromedriver对应下载地址 现在就可以使用 selenium 的 web

    2024年02月08日
    浏览(56)
  • 解决 Docker + selenium + chromedriver + chrome 会出现僵尸进程的问题

    在docker里,使用selenium爬虫,  webdriver quit 后,会产生很多僵尸进程。 docker run  - it  - v / home / blackip :/ home / blackips /    selenium : 1.0   python3 linux_black_ip . py top 查看僵尸进程: ps -ef | grep defunct 查看僵尸进程: 僵尸进程的父进程是python3。 看了下chrome运行时的状况,发现开始

    2023年04月24日
    浏览(28)
  • Chrome 115之后的版本,安装和使用chromedriver

    在Python中使用selenium 时报如下错误: 1. 老版本chrome对应的chromedriver 下载地址:CNPM Binaries Mirror 2. 新版本chrome对应的chromedriver 下载地址:Chrome for Testing availability

    2024年02月08日
    浏览(39)
  • CentOS7 启动谷歌浏览器 java+Selenium+chrome+chromedriver

    前言:自己想使用该技术实现自动化抓取音乐,目前在window上运行成功,需要在Linux Centos服务上跑,配置上出现了许多问题,特此记录。 参考文档:CentOS7 安装Selenium+chrome+chromedriver+java_远方丿的博客-CSDN博客  我们明确的是,在window上是安装了chrome和自带了chromeDriver的,之所

    2024年02月11日
    浏览(53)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包