Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load firefox profile with Python Selenium?

I'm trying to get Python Selenium to work on my Windows Machine. I've upgraded to the latest versions of Firefox, Selenium, Geckodriver, but I still receive the below error:

Python Script

from selenium import webdriver
driver = webdriver.Firefox()

Error

Traceback (most recent call last):
  File "run.py", line 17605, in <module>
  File "<string>", line 21, in <module>
  File "site-packages\selenium\webdriver\firefox\webdriver.py", line 77, in __init__
  File "site-packages\selenium\webdriver\firefox\extension_connection.py", line 49, in __init__
  File "site-packages\selenium\webdriver\firefox\firefox_binary.py", line 68, in launch_browser
  File "site-packages\selenium\webdriver\firefox\firefox_binary.py", line 103, in _wait_until_connectable
WebDriverException: Message: Can't load the profile. Profile Dir: %s If you specified a log_file in the FirefoxBinary constructor, check it for details.

I've also tried creating the firefox profile with the below code:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', ('application/vnd.ms-excel'))
profile.set_preference('general.warnOnAboutConfig', False)

gecko_path = "path_to_geckodriver\\geckodriver.exe"
path = "path_to_firefoxs\\Mozilla Firefox\\firefox.exe"
binary = FirefoxBinary(path)
driver = webdriver.Firefox(firefox_profile=profile,executable_path=gecko_path)
  • Python 2.7
  • Firefox 60
  • Geckodriver-v0.20.1-win64.zip
  • Selenium 3.12.0
like image 771
Chris Avatar asked May 13 '18 22:05

Chris


People also ask

How to create a Firefox profile for selenium test?

In order to run a successful Selenium Test, a Firefox profile should be – Let see step by step how to create a Firefox profile. In the first step, First of all close the Firefox if open. Step 2) Open Run (Windows key + R) and type firefox.exe –p Note: If it doesn’t open you can try using full path enclosed in quotes.

How can I use Python Selenium code with Firefox?

The code can then do anything you can do with a web browser, like opening a page, sending key presses or button clicks. To make Firefox work with Python selenium, you need to install the geckodriver.

How to create a profile in Selenium WebDriver using automation script?

Automation Script for Selenium. To access newly created Firefox profile in Selenium Webdriver software test, we need to use webdrivers inbuilt class 'profilesIni' and it's method getProfile as shown below. This is a code to implement a profile, which can be embedded in the selenium code.

How to create a Firefox profile?

How to create a Firefox profile. Let see step by step how to create a Firefox profile. Step 1) First of all close the Firefox if open. Step 2) Open Run (windows key + R) and type firefox.exe –p and click OK. Note: If it doesn't open you can try using full path enclosed in quotes.


1 Answers

Solution:

from selenium import webdriver
fp = webdriver.FirefoxProfile('/home/gabriel/.mozilla/firefox/whatever.selenium')
driver = webdriver.Firefox(fp)

I struggled until I found this issue which is now solved but was helpful because it shows some commands.

first version, not working because I could not connect with selenium afterward:

from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

options = Options()
options.add_argument("-profile")
options.add_argument("/home/gabriel/.mozilla/firefox/whatever.selenium")
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
driver = webdriver.Firefox(capabilities=firefox_capabilities, firefox_options=options)

I am sure that this loads the profile "whatever.selenium" because if I go to about:profiles I can read:

Profile: selenium This is the profile in use and it cannot be deleted.

Even though "whatever.selenium" is not the default profile on my system.

Remark: at least one the profile parameters is overridden by selenium (or geckodriver?): Preferences > Privacy and Security > "Block pop-up windows" is always reseted to off. So use about:profiles to make assertions on which profile you are running.

notes:

  • firefox_capabilities might not be needed in above code.
  • tested under Firefox 60.4.0esr (64-bit), geckodriver 0.23.0 ( 2018-10-04), selenium 3.141.0 with Python 3.5.3
like image 182
Gabriel Devillers Avatar answered Sep 21 '22 12:09

Gabriel Devillers