Reputation: 5282
Trying to add uBlock to a browser session but it's not working.
import selenium
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.firefox.options import Options as options
def establish_browser(type, hide):
browser = ''
if type == 'firefox':
ops = options()
ops.add_argument("--headless") if hide is True else ops.add_argument("--head")
profile = selenium.webdriver.FirefoxProfile()
profile.add_extension(extension='[email protected]')
browser = selenium.webdriver.Firefox(firefox_profile=profile, executable_path='geckodriver.exe', options=ops, firefox_binary=FirefoxBinary('C:/Program Files/Mozilla Firefox/firefox.exe'))
return browser
browser = establish_browser('firefox', False)
How should this be changed so uBlock works?
UPDATE
The chrome version appears to be working …
if type == 'chrome':
from selenium.webdriver.chrome.options import Options as options
ops = options()
ops.add_argument("--headless") if hide is True else ops.add_argument("--head")
ops.add_extension("ublock.crx")
browser = selenium.webdriver.Chrome(executable_path='chromedriver.exe', options=ops, desired_capabilities={'binary_location': 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe'})
is Firefox depreciated?
Upvotes: 3
Views: 3178
Reputation: 61
To add to @Rhys' solution, an easier approach might be the following option from the official documentation which works as expected:
driver = webdriver.Firefox('path/to/executable')
driver.install_addon('~/path/to/addon.xpi')
Upvotes: 0
Reputation: 5282
Since, for some reason, chrome's add_extension
works but firefox's add_extension
does not work (currently) … here is my workaround for adding extensions to firefox.
right click windows start button > run > firefox.exe -P
profile = selenium.webdriver.FirefoxProfile("C:/test")
browser = selenium.webdriver.Firefox(firefox_profile=profile, options=ops)
Apparently profile.add_extension()
is not a must have for this workaround
UPDATE! - added chrome profile
For symmetry purposes I have updated the chrome example code to use the chrome profile instead of calling the .crx
directly.
navigate to C:\Users\User\AppData\Local\Google\Chrome
or where-ever chromes User Data
folder is located. Call this folder directly (absolute path) or rename it and call the relative path. I have renamed it to chrome_profile
:
ops = options()
ops.add_argument("--headless") if hide is True else ops.add_argument("--head")
ops.add_argument('user-data-dir=chrome_profile')
ops.add_argument('--profile-directory=Default')
ops.add_argument("--incognito")
browser = selenium.webdriver.Chrome(executable_path='chromedriver.exe', options=ops, desired_capabilities={'binary_location': 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe'})
Upvotes: 3