21

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
Chris
  • 5,444
  • 16
  • 63
  • 119
  • See also [How to save and load cookies using Python + Selenium WebDriver - Stack Overflow](https://stackoverflow.com/questions/15058462/how-to-save-and-load-cookies-using-python-selenium-webdriver/65535817#65535817) for how to save. – user202729 Oct 27 '21 at 13:54

5 Answers5

18

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
Gabriel Devillers
  • 3,155
  • 2
  • 30
  • 53
  • So you have two blocks of code here, and the first one is supposed to be the solution, but it doesn't work because you don't specify the driver location anywhere. You know.. the geckodriver? – Eight Rice Dec 10 '20 at 12:30
  • Please ignore the second block of code. In my code the line that creates the driver is: `driver = webdriver.Firefox(fp, options=options, executable_path="path-to-geckodriver")` – Gabriel Devillers Dec 10 '20 at 13:37
7

On windows I use:

fp = webdriver.FirefoxProfile('C:/Users/x/AppData/Roaming/Mozilla/Firefox/Profiles/some-long-string')
driver = webdriver.Firefox(firefox_profile=fp)
...

1 - To find the current Profile Folder, type about:support on the url field and press enter.
2 - To see all user profiles type about:profiles on the url field and press enter.

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • 1
    This worked for me, however as a note, use the "root directory" not the local one. I kept trying to use the local directory and this never worked. Once I switched to the root, it worked perfectly. You can find your root and local directory locations by opening up Firefox and putting "about:profiles" in your browser. – Dave Feb 26 '21 at 15:10
3

You didn't update preferences for profile: profile.update_preferences(). You have to update the profile after set preference. Please following the code below:

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)
profile.update_preferences()
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)
Sang9xpro
  • 435
  • 5
  • 8
-1

Switch to the chrome driver. Firefox, gecko, and selenium are not working well together right now. Here is what finally worked for me.

import unittest
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

class TestTemplate(unittest.TestCase):
    """Include test cases on a given url"""

    def setUp(self):
        """Start web driver"""
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument('--no-sandbox')
        self.driver = webdriver.Chrome(chrome_options=chrome_options)
        self.driver.implicitly_wait(10)

    def tearDown(self):
        """Stop web driver"""
        self.driver.quit()

    def test_case_1(self):
        """Go to python.org and print title"""
        try:
            self.driver.get('https://www.python.org/')
            title = self.driver.title
            print title
        except NoSuchElementException as ex:
            self.fail(ex.msg)

    def test_case_2(self):
        """Go to redbull.com and print title"""
        try:
            self.driver.get('https://www.redbull.com')
            title = self.driver.title
            print title
        except NoSuchElementException as ex:
            self.fail(ex.msg)

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(TestTemplate)
    unittest.TextTestRunner(verbosity=2).run(suite)

I use a Jenkinsfile that loads the frame buffer to call selenium and the python script.

You could easily run this on your local machine. You may want to get a vagrant box going with linux.

Here is what launches the python script.

sh "(Xvfb :99 -screen 0 1366x768x16 &) && (python ./${PYTHON_SCRIPT_FILE})"

This is called from a docker file running this Docker image.

https://github.com/cloudbees/java-build-tools-dockerfile

James Knott
  • 1,278
  • 7
  • 6
-1

Loading customized firefox profile in java:

FirefoxOptions options = new FirefoxOptions();

ProfilesIni allProfiles = new ProfilesIni();         
FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile"); // manualy created profile in firefox profile manager
options.setProfile(selenium_profile);

options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", "C:\\Users\\pburgr\\Desktop\\geckodriver-v0.20.0-win64\\geckodriver.exe");
driver = new FirefoxDriver(options);
driver.manage().window().maximize();
pburgr
  • 1,722
  • 1
  • 11
  • 26