Automatically Download chromedriver that supports the currently installed version of chrome
Sebastian Wright
I'm trying to download Chromedriver into the current directory. I've tried chromedriver-auto installer and get_chrome_driver library. But, I can't download it into the specific directory.
This is my code.
from get_chrome_driver import GetChromeDriver
get_driver = GetChromeDriver()
get_driver.auto_download(extract=True, output_path="")and I got this
PS D:\Project\Python\Automation\Socinet> & C:/Python37/python.exe d:/Project/Python/Automation/Socinet/chromedriver.py
Traceback (most recent call last): File "d:/Project/Python/Automation/Socinet/chromedriver.py", line 4, in <module> get_driver.auto_download(extract=True, output_path="")
TypeError: auto_download() got an unexpected keyword argument 'output_path'
PS D:\Project\Python\Automation\Socinet> What should I do?
12 Answers
First run:
pip install get-chrome-driver --upgradethen give dot if you want to download to currrent directory
get_driver.auto_download(extract=True, output_path=".") You could use this solution. It will do two things:
- Check locally in your computer for the driver version and compare it with the latest version available online.
- The latest online version will be downloaded if it does not match your local version.
from selenium import webdriver
import requests
import zipfile
import wget
import subprocess
import os
CHROMEDRIVER_PATH = "" # Insert your Chromedriver path here
CHROMEDRIVER_FOLDER = os.path.dirname(CHROMEDRIVER_PATH)
LATEST_DRIVER_URL = ""
def download_latest_version(version_number): print("Attempting to download latest driver online......") download_url = "" + version_number + "/chromedriver_win32.zip" # download zip file latest_driver_zip = wget.download(download_url, out=CHROMEDRIVER_FOLDER) # read & extract the zip file with zipfile.ZipFile(latest_driver_zip, 'r') as downloaded_zip: # You can chose the folder path to extract to below: downloaded_zip.extractall(path=CHROMEDRIVER_FOLDER) # delete the zip file downloaded above os.remove(latest_driver_zip)
def check_driver(): # run cmd line to check for existing web-driver version locally cmd_run = subprocess.run("chromedriver --version", capture_output=True, text=True) # Extract driver version as string from terminal output local_driver_version = cmd_run.stdout.split()[1] print(f"Local driver version: {local_driver_version}") # check for latest chromedriver version online response = requests.get(LATEST_DRIVER_URL) online_driver_version = response.text print(f"Latest online chromedriver version: {online_driver_version}") if local_driver_version == online_driver_version: return True else: download_latest_version(online_driver_version)