PYTHON
Edit1:
Вы можете использовать webdriver_manager
для обработки этого сценария, где он позаботится о os.chomp
тоже. Вот установка в пипсах для webdriver_manager
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()
Вот грязный код для динамической загрузки последней версии chromedriver для windows.
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 для 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 исключение версии и восстановление выполнения после загрузки хроматического драйвера. Таким образом, сценарии мои сценарии никогда не завершаются ошибкой из-за изменений версии chrome.