在 Python 中为 Selenium 自动下载适当的 chromedriver [英] Automatic download of appropriate chromedriver for Selenium in Python

查看:104
本文介绍了在 Python 中为 Selenium 自动下载适当的 chromedriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遗憾的是,Chromedriver 始终是特定于您安装的 Chrome 版本的版本.因此,当您通过 PyInstaller 在 Windows 的可部署 .exe 文件中打包您的 python 代码和 chromedriver 时,它在大多数情况下不起作用,因为您将无法在 .exe 文件中拥有所有 chromedriver 版本.

有人知道如何从网站上自动下载正确的 chromedriver 吗?

如果没有,我会想出一个代码来下载 zip 文件并将其解压缩到临时.

谢谢!

解决方案

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

导入请求导入 wget导入压缩文件导入操作系统# 获取最新的chrome驱动版本号url = 'https://chromedriver.storage.googleapis.com/LATEST_RELEASE'响应 = requests.get(url)version_number = response.text# 构建下载地址download_url = "https://chromedriver.storage.googleapis.com/" + version_number +"/chromedriver_win32.zip"# 使用上面构建的 url 下载 zip 文件latest_driver_zip = wget.download(download_url,'chromedriver.zip')# 解压压缩文件使用 zipfile.ZipFile(latest_driver_zip, 'r') 作为 zip_ref:zip_ref.extractall() #这里可以指定目标文件夹路径# 删除上面下载的zip文件os.remove(latest_driver_zip)

Unfortunately, Chromedriver always is version-specific to the Chrome version you have installed. So when you pack your python code AND a chromedriver via PyInstaller in a deployable .exe-file for Windows, it will not work in most cases as you won't be able to have all chromedriver versions in the .exe-file.

Anyone knows a way on how to download the correct chromedriver from the website automatically?

If not, I'll come up with a code to download the zip-file and unpack it to temp.

Thanks!

解决方案

Here is the other solution, where webdriver_manager does not support. This script will get the latest chrome driver version downloaded.

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)

这篇关于在 Python 中为 Selenium 自动下载适当的 chromedriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆