Python Friday #143: Driver Manager for Selenium

Downloading drivers for Selenium is annoying. We need to find the right driver for the specific browser version that runs on our computer. Luckily for us, there is a project that can do this work for us.

This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.

 

Installation

We can install the Webdriver Manager with this command:

 

Install Firefox automatically

If we do not want to download the geckodriver by hand, we can change our code to start Selenium to this:

We do no longer need to point to the geckodriver.exe and instead let the Webdriver Manager do all the work.

 

Fix the GitHub rate limit

The code above works if you run the script at the beginning of the rate limit window of GitHub. A more likely outcome will be an exception with this message:

ValueError: API Rate limit exceeded. You have to add GH_TOKEN!!!

This happens because GitHub hosts the geckodriver and has a strict rate limit for unauthenticated users. We need to create a personal access token on GitHub that needs no rights for our user. It is enough to identify ourselves towards GitHub. The token we generate goes into the .env file inside our project:

GH_TOKEN=YOUR_GITHUB_TOKEN

If we now run our script, it should download the driver if it is not already present, open Firefox, go to duckduckgo.com and read the page title:

DuckDuckGo — Privacy, simplified.

 

Turn off the annoying debug messages

If you take a closer look to the output of the script, you will notice a lot of messages like this one:

[WDM] – ====== WebDriver manager ======
[WDM] – Current firefox version is 104.0
[WDM] – Get LATEST geckodriver version for 104.0 firefox

While those messages are great to find a problem, they are annoying when you need to concentrate on the output of your script. You can modify the logger to block the WDM messages with this code right after the import statements:

 

Next

We have now a simple solution in place that takes care of downloading the right drivers without any additional work for us. Next week we can start automating a browser from our Python code through Selenium.

2 thoughts on “Python Friday #143: Driver Manager for Selenium”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.