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:
1 |
pip install webdriver-manager |
Install Firefox automatically
If we do not want to download the geckodriver by hand, we can change our code to start Selenium to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from selenium.webdriver.firefox.service import Service from selenium import webdriver from webdriver_manager.firefox import GeckoDriverManager import time import os from dotenv import load_dotenv # read value for GH_TOKEN key from .evn file load_dotenv() driver = webdriver.Firefox(service=Service(GeckoDriverManager().install())) driver.implicitly_wait(1) driver.get("https://duckduckgo.com/?t=ha&va=j") title = driver.title print(title) time.sleep(5) driver.quit() |
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:
1 2 3 |
import logging logging.getLogger('WDM').setLevel(logging.NOTSET) |
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”