Hi everyone, this evening I tried messing around with selenium for python. I want to share with you some little tricks that helped me saving a lot of time.
Wait, what is selenium?
Selenium is a tool that is designed to allow scripting operations using a lot of browsers. Therefore, it is used widely in many scenarios, like automatic testing, web scraping, security testing, so on and so forth. The cool thing with that tool is that you can easily open multiple different browsers at the same time, performing the same exact operations. Maybe I will cover some basic tutorial on how to use selenium in the next articles.
Some of you, interested in using selenium, might have found some difficulties during the loading step. In fact, while the Chrome setup is quite straightforward, using Firefox (especially if you are on Windows!) might cause you a little headache due to PATH environment and drivers.
The solution is using webdriver-manager, a simple python package that allows you to download and cache needed drivers to be good to go in a few seconds! Cool, isn’t it?
First of all, let’s keep your environment clean. I mean, your virtual environment (at least). Create from scratch a virtual environment (for Windows I use
pip install virtualenvwrapper-win, while for Linux you might use
pip install virtualenvwrapper) using
Now we are good to go with our tutorial. Install required packages:
pip install selenium pip install webdriver-manager
Then you can simply run selenium into the browser of your choice using a few lines of codes, without installing drivers, extensions and other strange and time-consuming tricks. I also included some test lines just to try that it is actually working.
from selenium import webdriver from webdriver_manager.firefox import GeckoDriverManager import time # Setup drivers firefox = webdriver.Firefox(executable_path=GeckoDriverManager().install()) # Get a random URL, wait 2 seconds, close the browser firefox.get('https://www.duckduckgo.com/') time.sleep(2) firefox.close()
from selenium import webdriver from webdriver_manager.microsoft import EdgeChromiumDriverManager import time # Setup drivers edge = webdriver.Edge(EdgeChromiumDriverManager().install()) # Get a random URL, wait 2 seconds, close the browser edge.get('https://www.duckduckgo.com/') time.sleep(2) edge.close()
from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager import time # Setup drivers chrome = webdriver.Chrome(ChromeDriverManager().install()) # Get a random URL, wait 2 seconds, close the browser chrome.get('https://www.duckduckgo.com/') time.sleep(2) chrome.close()
If you take a look at the official documentation of webdriver-manager, you will find the needed inspiration to make it working for even more browsers.
I hope you enjoyed this short tutorial! If you have any questions, don’t hesitate to write below in the comment section.