为了账号安全,请及时绑定邮箱和手机立即绑定

在 Python 中自动下载适合 Selenium 的 chromedriver

在 Python 中自动下载适合 Selenium 的 chromedriver

慕田峪4524236 2022-12-06 16:34:21
不幸的是,Chromedriver 总是特定于您安装的 Chrome 版本。因此,当您通过 PyInstaller 将 python 代码和 chromedriver 打包到 Windows 的可部署 .exe 文件中时,它在大多数情况下都不起作用,因为您将无法在 .exe 文件中拥有所有 chromedriver 版本。任何人都知道如何从网站自动下载正确的 chromedriver 的方法?如果没有,我会想出一个代码来下载 zip 文件并将其解压缩到临时文件。谢谢!
查看完整描述

5 回答

?
胡说叔叔

TA贡献1804条经验 获得超8个赞

这是另一个解决方案,webdriver_manager不支持。此脚本将下载最新的 chrome 驱动程序版本。


import requests

import wget

import zipfile

import os


# get the latest chrome driver version number

url = 'https://chromedriver.storage.googleapis.com/LATEST_RELEASE'

response = requests.get(url)

version_number = response.text


# build the donwload url

download_url = "https://chromedriver.storage.googleapis.com/" + version_number +"/chromedriver_win32.zip"


# download the zip file using the url built above

latest_driver_zip = wget.download(download_url,'chromedriver.zip')


# extract the zip file

with zipfile.ZipFile(latest_driver_zip, 'r') as zip_ref:

    zip_ref.extractall() # you can specify the destination folder path here

# delete the zip file downloaded above

os.remove(latest_driver_zip)


查看完整回答
反对 回复 2022-12-06
?
倚天杖

TA贡献1828条经验 获得超3个赞

这是 Python 的另一种方法 -


import chromedriver_autoinstaller

from selenium import webdriver


opt = webdriver.ChromeOptions()

opt.add_argument("--start-maximized")


chromedriver_autoinstaller.install()

driver = webdriver.Chrome(options=opt)

driver.get('https://stackoverflow.com/')

这里有更多信息


https://pypi.org/project/chromedriver-autoinstaller/


查看完整回答
反对 回复 2022-12-06
?
猛跑小猪

TA贡献1858条经验 获得超8个赞

Webdriver Manager 会为你做这件事。请参考此链接https://pypi.org/project/webdriver-manager/


查看完整回答
反对 回复 2022-12-06
?
饮歌长啸

TA贡献1951条经验 获得超3个赞

我有一个稍微漂亮一点的版本


它会检测到你的 chrome 版本,获取正确的驱动程序


def download_chromedriver():

    def get_latestversion(version):

        url = 'https://chromedriver.storage.googleapis.com/LATEST_RELEASE_' + str(version)

        response = requests.get(url)

        version_number = response.text

        return version_number

    def download(download_url, driver_binaryname, target_name):

        # download the zip file using the url built above

        latest_driver_zip = wget.download(download_url, out='./temp/chromedriver.zip')


        # extract the zip file

        with zipfile.ZipFile(latest_driver_zip, 'r') as zip_ref:

            zip_ref.extractall(path = './temp/') # you can specify the destination folder path here

        # delete the zip file downloaded above

        os.remove(latest_driver_zip)

        os.rename(driver_binaryname, target_name)

        os.chmod(target_name, 755)

    if os.name == 'nt':

        replies = os.popen(r'reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version').read()

        replies = replies.split('\n')

        for reply in replies:

            if 'version' in reply:

                reply = reply.rstrip()

                reply = reply.lstrip()

                tokens = re.split(r"\s+", reply)

                fullversion = tokens[len(tokens) - 1]

                tokens = fullversion.split('.')

                version = tokens[0]

                break

        target_name = './bin/chromedriver-win-' + version + '.exe'

        found = os.path.exists(target_name)

        if not found:

            version_number = get_latestversion(version)

            # build the donwload url

            download_url = "https://chromedriver.storage.googleapis.com/" + version_number +"/chromedriver_win32.zip"

            download(download_url, './temp/chromedriver.exe', target_name)


    elif os.name == 'posix':

        reply = os.popen(r'chromium --version').read()


        if reply != '':

            reply = reply.rstrip()

            reply = reply.lstrip()

            tokens = re.split(r"\s+", reply)

            fullversion = tokens[1]

            tokens = fullversion.split('.')

            version = tokens[0]

        else:

            reply = os.popen(r'google-chrome --version').read()

            reply = reply.rstrip()

            reply = reply.lstrip()

            tokens = re.split(r"\s+", reply)

            fullversion = tokens[2]

            tokens = fullversion.split('.')

            version = tokens[0]


        target_name = './bin/chromedriver-linux-' + version

        print('new chrome driver at ' + target_name)

        found = os.path.exists(target_name)

        if not found:

            version_number = get_latestversion(version)

            download_url = "https://chromedriver.storage.googleapis.com/" + version_number +"/chromedriver_linux64.zip"

            download(download_url, './temp/chromedriver', target_name) 

 


查看完整回答
反对 回复 2022-12-06
?
明月笑刀无情

TA贡献1828条经验 获得超4个赞

截至 2021 年 8 月,这是状态:-


chromedriver-自动安装程序

自动下载并安装支持当前安装的 chrome 版本的 chromedriver。此安装程序支持 Linux、MacOS 和 Windows 操作系统。


安装

pip install chromedriver-autoinstaller

用法

Just type import chromedriver_autoinstaller in the module you want to use chromedriver.

例子

from selenium import webdriver

import chromedriver_autoinstaller



chromedriver_autoinstaller.install()  # Check if the current version of chromedriver exists

                                      # and if it doesn't exist, download it automatically,

                                      # then add chromedriver to path


driver = webdriver.Chrome()

driver.get("http://www.python.org")

assert "Python" in driver.title


查看完整回答
反对 回复 2022-12-06
  • 5 回答
  • 0 关注
  • 324 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号