Reputation: 5922
The last version I used of Selenium was 3.141.0.
I am now transitioning to Selenium 4, and I am trying to figure out how to add extensions for Firefox, in Python 3.
Previously, something like this would work:
from selenium import webdriver
profile = webdriver.FirefoxProfile(profile_path)
profile.add_extension('adblock_plus-3.10.2-an+fx.xpi')
driver = webdriver.Firefox(profile)
While attempting to make some adjustments that are apparently needed for Selenium 4, I tried the following code:
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
options = Options()
options.set_preference('profile', profile_path)
options.add_extension('adblock_plus-3.10.2-an+fx.xpi')
service = Service('/usr/local/bin/geckodriver')
driver = webdriver.Firefox(options=options, service=service)
In Selenium 4.1.0, this gives AttributeError
:
options.add_extension('adblock_plus-3.10.2-an+fx.xpi') AttributeError: 'Options' object has no attribute 'add_extension'
Using .add_extension()
with an Options()
object is apparently not the correct way to add an extension. However, I am unable to find the right way.
It is potentially possible to create a FirefoxProfile
and add the extension there, but at very least that seems to give a DeprecationWarning
, and I don't know if it will work anyhow:
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
profile = webdriver.FirefoxProfile(profile_path)
profile.add_extension('adblock_plus-3.10.2-an+fx.xpi')
options = Options()
options.set_preference('profile', profile_path)
service = Service('/usr/local/bin/geckodriver')
driver = webdriver.Firefox(profile, options=options, service=service)
DeprecationWarning: profile has been deprecated, please pass in an Options object driver = webdriver.Firefox(profile, options=options, service=service)
What is the proper way to add extensions in Selenium 4 for Firefox?
Upvotes: 3
Views: 2225
Reputation: 689
You should be able to add an extension directly on the Webdriver
driver = webdriver.Firefox(service=service)
driver.install_addon('adblock_plus-3.10.2-an+fx.xpi')
Upvotes: 1