Python Selenium browser with extensions - python

I can't seem to find any information on how to set python to open a browser (firefox) but a version that has an extension installed such as adblock?
Thanks.

Instantiate FirefoxProfile() and use add_extension() to add an existing extension:
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.add_extension('path_to_extension')
profile.set_preference("extensions.adblockplus.currentVersion", "2.4")
driver = webdriver.Firefox(firefox_profile=profile)

Related

How can I choose which firefox executable to use among multiple firefox binaries through Python selenium

I'm using python (v 3.65) selenium (v3.11.0) on Mac OS X (v 10.11.6) with firefox (v 59.0.3) and geckodriver (v 0.20.1). I have my usual firefox in the Applications folder and a second firefox in another folder. How can I tell python selenium to use the second firefox instead of going to the one in Applications?
I'd prefer an answer that generalises to other browsers besides firefox/geckodriver, if possible.
To choose and use one of the Firefox executable among multiple you can use the argument binary_location from firefox.options. As an example in the following code block I have used the Firefox Nightly binary to open the Firefox Nightly browser :
Code Block :
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.binary_location = r'C:\Program Files\Firefox Nightly\firefox.exe'
driver = webdriver.Firefox(firefox_options=options, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get('http://google.com/')
print("Firefox Browser Invoked")
driver.quit()
Console Output :
Firefox Browser Invoked

How can I use selenium in Raspbian python3?

I'm using:
Raspberry Pi3
OS : Raspbian
language : python 3.5
Geckodriver : geckodriver-v0.19.1-arm7hf (usr/local/bin/geckodriver)
browser firefox esr (52.6.0 (32-bit))
Code:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.python.org')
Opening the browser is OK, but why did it fail to get URL?
I don't know how to solve it.
While you initialize the webdriver and the Web Browser pass the argument executable_path along with the absolute path of the GeckoDriver binary and invoke the proper url as follows :
from selenium import webdriver
driver = webdriver.Firefox(executable_path='usr/local/bin/geckodriver')
driver.get("your_url")

selenium 3.6.0 and extensions

Note that I edited the question.
How to activate blocking for ghostery extension in firefox through selenium python bindings? Note that by default the blocking in ghostery is disabled.
I am using selenium 3.6.0 and ghostery 7.3.3.7. Note that I am using selenium + pull request #4790 that allow the new web extensions, because the released version lack this feature.
I do the following:
from selenium import webdriver
fp = webdriver.FirefoxProfile()
fp.add_extension(extension=/home/nsarafij/ghostery-7.3.3.7.xpi)
driver = webdriver.Firefox(firefox_profile = fp)
Is it possible to activate blocking in ghostery through setting preferences?
fp.set_preferances(...)
Or, I have to do something different?
I am not sure i understand you correctly - you want to add Ghostery to Firefox right?
If so, this should work:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
firefoxProfile = FirefoxProfile()
ghostery="PATH_TO_GHOSTERY_ADDON"
firefoxProfile.add_extension(ghostery)
firefoxProfile.update_preferences()
driver = webdriver.Firefox(firefoxProfile)

Python Selenium Firefox driver - Disable Images

Before I have used the code below, but it doesn't work anymore with firefox update.
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
firefoxProfile = FirefoxProfile()
firefoxProfile.set_preference('permissions.default.image', 2)
I also tried this one below, it seems good but is there a way to disable images without add-on or 3rd party tools?
from selenium import webdriver
firefox_profile = webdriver.FirefoxProfile()
firefox_profile.add_extension(folder_xpi_file_saved_in + "\\quickjava-2.0.6-fx.xpi")
firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.Images", 2) ## Turns images off
Have you tried updating your selenium after the Firefox update?
eg :
sudo -H pip install --upgrade selenium

Running headless firefox with selenium in Linux

I am trying to run a headless Firefox browser on Linux. I have firefox installed and on my PATH, xvfb is installed, and am using pyvirtualdisplay to setup the display with xvfb. When the last line is executed
from pyvirtualdisplay import Display
from selenium import webdriver
display = Display(visible=False, size=(1024, 768))
display.start()
browser = webdriver.Firefox()
I get the error message:
WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.
I tried setting a log file as:
p = webdriver.FirefoxProfile()
p.set_preference("webdriver.firefox.logfile", "/tmp/firefox_log")
browser = webdriver.Firefox(p)
But there is no log file created (and creating the file first does not write to it). How do you find out more information for what is going wrong? How do I fix this?
from pyvirtualdisplay import Display
from selenium import webdriver
class Firefox:
def __init__(self):
self.display = Display(visible=0, size=(800, 600))
self.display.start()
self.driver = webdriver.Firefox()
self.driver.set_window_size(1120, 450)
def shutdown(self):
self.display.stop()
self.driver.quit()
I think python is not pointed towards the correct binary. I had some issues in the past like that after I deleted the binary that came with my distro and then installing the 64bit version. Yes, there are issues with 64bit version in Linux, based on my experience. Usually opens and hangs there doing nothing.
If that's your problem, get a 32bit prior to 45ff version if your testings are not based gecko driver. For ff45+ get also the gecko driver and add the binary to path(of gecko driver). Then you should use Firefox Binary this way.
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary("/usr/bin/firefox") #Or whatever path you have(E.G. Portable)
driver = webdriver.Firefox(firefox_binary=binary)
On windows I use portable apps. On linux you have to make the binary portable, there is a thread, here on Stack, about that. Altough is not necesary
Cheers

Categories

Resources