Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic download of appropriate chromedriver for Selenium in Python

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!

like image 654
tim Avatar asked May 26 '20 07:05

tim


People also ask

Why is selenium chromedriver not working in Python?

Note, loading Selenium requires having chromedriver in the same directory as the Python script. If it fails to load, this is usually due to either the file msising or the version of Chrome on the computer being a higher version than the driver. # Attempt to open the Selenium chromedriver. If it fails, download the latest chromedriver.

Can I use chromedriver with pyinstaller?

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.

Does chromedriver work with Python code?

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 W... Stack Overflow About Products For Teams Stack OverflowPublic questions & answers

How do I run selenium on Linux?

Open Selenium using Chrome in headless mode. If step 1 fails, automatically find the latest driver version for Linux, Mac, or Windows. Download. Unzip. Set executable permissions. Reload Selenium. Note, loading Selenium requires having chromedriver in the same directory as the Python script.


5 Answers

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)
like image 102
supputuri Avatar answered Oct 22 '22 22:10

supputuri


Webdriver Manager will do that for you. refer this link https://pypi.org/project/webdriver-manager/

like image 7
NarendraR Avatar answered Oct 22 '22 20:10

NarendraR


Here is one more way For 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/')

here is more info

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

like image 6
Hietsh Kumar Avatar answered Oct 22 '22 20:10

Hietsh Kumar


I have a slightly fancier version

It will detected what chrome version you have, grab the correct driver

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) 
 
like image 5
Hairy Ass Avatar answered Oct 22 '22 22:10

Hairy Ass


As of Aug-2021, this is the status :-

chromedriver-autoinstaller

Automatically download and install chromedriver that supports the currently installed version of chrome. This installer supports Linux, MacOS and Windows operating systems.

Installation

pip install chromedriver-autoinstaller

Usage

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

Example


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

Reference link here

like image 5
cruisepandey Avatar answered Oct 22 '22 21:10

cruisepandey