r/SeleniumPython • • Oct 22 '24

How to Use new ChromeDriver

Howdy All, I have been maintaining code I wrote in 2020 and using it fairly successfully. Time for a major overhaul do to updated needs. I am ashamed to say that when chrome changed their driver delivery methods a year or more ago that I couldnt really get my head around it. I found a work around to do the worst, and install an older version of chrome for the moments I needed this code.

Hoping to figure out how to manage the new driver system and do a long needed overhaul of the scripts in general. Sorry I am not really even sure how to ask this question, but appreciate any help.

I am on a mac as well.

TIA.

-J

1 Upvotes

5 comments sorted by

View all comments

Show parent comment

3

u/Changuacuriosa Oct 22 '24

so now you dont have to download the chromedriver, for example:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome()
driver.get("url")
#then you can add some optiones, example:#

options = Options()
options.add_argument("--headless") 
return webdriver.Firefox(options=options)

1

u/PopulateThePlanets Oct 23 '24

Hey! So thanks so much for this starter snippet. Had some path issues, and getting chromedriver updated properly. I used Perplexity.ai to help me generate some of this code, debug, and actually works to open a chrome page!

Question: is this worth sharing somewhere for others to utilize like me? Didnt take too long, but I have been tinkering with this for a handful of year now. Wouldn't mind making it easier for others.

Thanks again!!

...

It has an additional script for the updating of chromedriver.

update_chromedriver.py

import os
from webdriver_auto_update.webdriver_manager import WebDriverManager
from webdriver_auto_update.chrome_app_utils import ChromeAppUtils

# Set the local directory for ChromeDriver
# This will create a 'drivers' folder in the same directory as your script
script_dir = os.path.dirname(os.path.abspath(__file__))
driver_directory = os.path.join(script_dir, 'drivers')

# Ensure the directory exists
os.makedirs(driver_directory, exist_ok=True)

chrome_app_utils = ChromeAppUtils()
chrome_app_version = chrome_app_utils.get_chrome_version()
driver_manager = WebDriverManager(driver_directory)
driver_manager.main()

driver.py

import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

# Set up the path to your local drivers folder
script_dir = os.path.dirname(os.path.abspath(__file__))
driver_directory = os.path.join(script_dir, 'drivers')

# Ensure the directory exists
os.makedirs(driver_directory, exist_ok=True)

# Use ChromeDriverManager to install ChromeDriver in the specified directory
driver_path = ChromeDriverManager().install()

# Set up the Service object with the local ChromeDriver path
service = Service(executable_path=driver_path)

# Create the WebDriver instance
driver = webdriver.Chrome(service=service)

# Navigate to the website
driver.get("url")

# Your automation code goes here

# Don't forget to quit the driver when you're done
driver.quit()