Firefox is my personal favorite browser, due in part to all of the great extensions available for it. When you try running Firefox with Selenium, however, you’ll probably find that Firefox is missing the extensions you have installed and normally use when browsing. Luckily, there’s a quick and easy way to install all your favorite Firefox extensions when using Selenium.
For example, let’s say we’d like to do a little light web scraping. To keep things simple, let’s just grab what’s trending off of Yahoo’s home page.
Scraping websites content on demand. Our Web Scraping API and Tools are built for everyone, from data scientist to a developer. Start crawling and scraping websites in minutes thanks to our APIs created to open your doors to internet data freedom. We offer web scraping APIs for developers & web scraper for chrome & firefox for Non-Developers. Alternatively, you can get to the same place by entering about:support in your Firefox navigation bar. In the “Application Basics” section click the “Open Directory” button, and in the file browser that pops up open the “extensions” folder. These are the extension installation files we’ll need to reference in our script.
You can see the top 10 trending subjects off to the right, starting with Beaufort County.
Selenium without Firefox Extensions
Here’s how we’d normally scrape that info:
2 4 6 8 10 12 14 16 | geckodriver = 'C:UsersGraysonDownloadsgeckodriver.exe' browser = webdriver.Firefox(executable_path=geckodriver) browser.get('http://www.yahoo.com') trending_xpath = '//li[@class='trending-list selected']/ul/li/a/span' trending = browser.find_elements_by_xpath(trending_xpath) # trending # and subject are separate elements, concatenate like so print(trending[i].text + trending[i+1].text) browser.quit() |
In our case though, the extension and file names aren’t too hard to match:
- HTTPS Everywhere: https-everywhere@eff.org.xpi
- uBlock Origin: uBlock0@raymondhill.net.xpi
Selenium with Firefox Extensions
Now we just need to add a few lines of code to our original script to install these extensions. We’ll perform the installations right after we initialize the browser.
Run this script and see if you get the same results as last time. Look for the extension symbols near the top right of the browser. You should see the blue-and-white “S” symbol for HTTPS Everywhere and the reddish badge symbol for uBlock Origin.
2 4 6 8 10 12 14 16 18 20 22 24 26 | geckodriver = 'C:UsersGraysonDownloadsgeckodriver.exe' browser = webdriver.Firefox(executable_path=geckodriver) extension_dir = 'C:UsersGraysonAppDataRoamingMozillaFirefoxProfiles3rqg4psi.defaultextensions' # remember to include .xpi at the end of your file names 'https-everywhere@eff.org.xpi', ] for extension in extensions: browser.install_addon(extension_dir + extension, temporary=True) browser.get('http://www.yahoo.com') trending_xpath = '//li[@class='trending-list selected']/ul/li/a/span' trending = browser.find_elements_by_xpath(trending_xpath) for i in range(0, 20, 2): |