How to run Selenium using Python on Raspberry Pi
I spent a day looking for a solution to get Selenium working on Raspberry Pi. Hopefully, this will also work for you and save you hours searching internet.
Selenium requires to have installed a browser that is controlled by a driver. As browser I use Chromium which can be downloaded by this command.
sudo apt-get install chromium-browser
Now that you have Chromium browser installed you need to find which version you are running.
It should return something similar to this.
Chromium 78.0.3904.108 Built on Raspbian, running on Raspbian 10
After you know which Chromium version you have installed, you have to find chromedriver which supports the same version as the Chromium you have. Prepare for a few trials and errors because now you have to open https://github.com/electron/electron/tags and go through different releases to look for chromedriver in the assets. They use chromedriver for their project, which they regularly update and you can find there almost every version of it.
Download the armv7l version of and check if the chromedriver version matches the Chromium version and if not try a different one.
For downloading directly to Raspberry Pi use this command.
sudo wget https://github.com/electron/electron/releases/download/v9.0.2/chromedriver-v9.0.2-linux-armv7l.zip
For unziping you can use.
sudo apt-get install unzipunzip chromedriver-v9.0.2-linux-armv7l.zip
Do not forget to move the chromedriver to /usr/lib/chromium-browser/. After this everything is set up and it should work. If not then maybe you might need to set up a route to the chromedriver in Selenium.
driver = webdriver.Chrome(‘/usr/lib/chromium-browser/chromedriver’)
If you are using Raspberry Pi without a monitor, please read further.
Headless Raspberry Py?
Another problem I had is that Chromium requires a monitor to run otherwise it returns an error. This is how to fix it:
pip3 install pyvirtualdisplay
Now you can simulate a display in Python:
from pyvirtualdisplay import Displaydisplay = Display(visible=0, size=(800, 600))display.start()
If need to start chromedriver as sudo you need to run it with this extension:
You can avoid that by giving privileges the file you want to run.
chmod -R 777 /path/to/file