0

I am trying to use Selenium and Chromedriver in Python. I am currently using PyCharm. I have the chromedriver.exe file saved in my Downloads and it is up to date with the Chrome version I am using.

The file path is "C:\Users\ea.palacios\Downloads\chromedriver.exe'

My script reads as:

from selenium import webdriver
driver = webdriver.Chrome(executable_path='C:\\Users\\ea.palacios\\Downloads\\chromedriver.exe')

I have also tried removing the double back-slashes:

from selenium import webdriver
driver = webdriver.Chrome(executable_path='C:\Users\ea.palacios\Downloads\chromedriver.exe')

When I tried running either script, a browser briefly appeared for less then a second before immediately closing. Then PyCharm returned the following message:

Traceback (most recent call last):
  File "C:\Users\marcd.admin\PycharmProjects\PLDT\main.py", line 3, in <module>
    driver = webdriver.Chrome(executable_path='C:\\Users\\ea.palacios\\Downloads\\chromedriver.exe')
  File "C:\Users\marcd.admin\PycharmProjects\PLDT\venv\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 76, in __init__
    RemoteWebDriver.__init__(
  File "C:\Users\marcd.admin\PycharmProjects\PLDT\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Users\marcd.admin\PycharmProjects\PLDT\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Users\marcd.admin\PycharmProjects\PLDT\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\marcd.admin\PycharmProjects\PLDT\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from tab crashed
  (Session info: chrome=92.0.4515.107)

Here is a screenshot of code and error message

Could this be happening because I am running PyCharm as an Administrator on my work computer but the Chromedriver is saved in my regular downloads folder? Really no clue! Help! Please and thanks.

2 Answers2

0

Use :-

chromedriver-autoinstaller

i.e. :

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
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
0

You could use this solution. It will do two things:

  1. Check locally in your computer for the driver version and compare it with the latest version available online.
  2. The latest online version will be automatically 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 = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE"


def download_latest_version(version_number):
    print("Attempting to download latest driver online......")
    download_url = "https://chromedriver.storage.googleapis.com/" + 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)
Competency Test
  • 121
  • 2
  • 6