Python
编辑1:
您可以使用webdriver_manager 来处理这种情况,它也会处理os.chomp。这是webdriver_manager的pip安装
pip install webdriver-manager
下面是 chrome 的示例脚本。
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.google.com")
driver.quit
旧答案:
================================================ ==============
这将确保 chromedriver 始终是最新的稳定版本,并且您无需执行任何手动步骤
webdriver_manager 的另一个优势是您可以即时下载任何驱动程序。下面是 Firefox (GeckoDriver) 的简单示例。
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(executable_path =GeckoDriverManager().install())
driver.get("https://www.google.com")
driver.quit()
这是为 windows 动态下载最新 chromedriver 的脏代码。
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)
RUBY这是在 ruby for windows 中实现的解决方案。
require 'net/http'
require 'open-uri'
require 'zip'
# Method to extract the contents of the zip file to the destination path
def extract_zip(file, destination)
# create the destination folder if it's not exist
FileUtils.mkdir_p(destination)
Zip::File.open(file) do |zip_file|
zip_file.each do |f|
fpath = File.join(destination, f.name)
zip_file.extract(f, fpath) unless File.exist?(fpath)
end
end
end
# url where you can get the latest chrome driver information
url = 'https://chromedriver.storage.googleapis.com/LATEST_RELEASE'
# get the latest driver version
parsed_url = URI.parse(url)
http = Net::HTTP.new(parsed_url.host, parsed_url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
request = Net::HTTP::Get.new(url)
request["Accept"] = 'application/json'
response = http.request(request)
# build the download url based on the above step
download_url = "https://chromedriver.storage.googleapis.com/#{response.body}/chromedriver_win32.zip"
puts download_url
# build the temporary location where you want to store the donwload zip
download_path = File.join(ENV['TEMP'],response.body.gsub('.',"_")) + '.zip'
puts download_path
# download the zip file
File.open(download_path, "wb") do |file|
file.write open(download_url).read
end
# Extract the chromedriver.exe from the zip in specific location
extract_zip(download_path,"my_destination_folder_path") # don't specify the filename.
# delete the zip file
FileUtils.rm_rf(download_path)
这个 .rb 文件将在我收到驱动程序不支持 chrome 版本异常的错误时执行,并在下载 chromedriver 后恢复执行。这样,我的脚本就不会因为 chrome 版本的变化而失败。