9

There are about 100 posts about the same issue but none of them seem to work for me, hence asking again. I'm trying to launch a Firefox browser using Python and Selenium and I get the following error:

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 each and every answer on the web but nothing seems to work.

This is my code:

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

caps = DesiredCapabilities.FIREFOX
caps["marionette"] = False

binary = FirefoxBinary('d:\\Desktop\\IEDriver\\geckodriver.exe')

options = Options()
options.set_headless(headless=True)
driver = webdriver.Firefox(firefox_binary=binary, firefox_options=options, executable_path=r'd:\\Desktop\\IEDriver\\geckodriver.exe')
driver.get("http://google.com/")
print ("Headless Firefox Initialized")
driver.quit()

If I set caps["marionette"] = True then the error I get is

SessionNotCreatedException: Message: Unable to find a matching set of capabilities

Versions of software I'm running:

Firefox: 62.0 (64 bit)

Selenium: 3.14.0

Gecko: 0.21.0

Python: 3

OS: Windows 8.1 64 bit

Any help would be highly appreciated.

EDIT: I've uninstalled and re-installed Firefox but didn't work. Also tried installing Firefox 61.0.2, still no luck.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Anish
  • 740
  • 4
  • 11
  • 27
  • 1
    `firefox_binary` should be a path to your Firefox executable (`firefox.exe`), but not to `geckodriver.exe` – Andersson Sep 13 '18 at 19:31

6 Answers6

10

This 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.

...implies that the GeckoDriver was unable to initiate/spawn a new WebBrowser i.e. Firefox Browser session.

You need to take care of a couple of things as follows:

  • To set the FirefoxBinary you need to use the FirefoxOptions() and instead of passing the absolute path of geckodriver binary, you have to pass the absolute path of the desired firefox binary.
  • As you are using GeckoDriver v0.21.0 you have to mandatorily use marionette so either keep it unchanged (by default true) or set marionette to true.
  • Your own code with incorporating the minor changes will be:

    from selenium import webdriver
    from selenium.webdriver.firefox.options import Options
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    
    binary = r'C:\Program Files\Mozilla Firefox\firefox.exe'
    options = Options()
    options.set_headless(headless=True)
    options.binary = binary
    cap = DesiredCapabilities().FIREFOX
    cap["marionette"] = True #optional
    driver = webdriver.Firefox(firefox_options=options, capabilities=cap, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
    driver.get("http://google.com/")
    print ("Headless Firefox Initialized")
    driver.quit()
    
  • Console Output:

    Headless Firefox Initialized
    
  • Here you can find a detailed discussion on Unable to find a matching set of capabilities with selenium 3.4.3, firefox 54.0 and gecko driver 0.17

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I'm having the same problem as the OP, when I try to implement this solution I get `PermissionError: [Errno 13] Permission denied: 'geckodriver.log'`... – pd441 Oct 25 '21 at 13:43
0

Make sure (especially on Windows (Win 10)) that your browser and controller (python/C/java/perl/etc) is all either x64 or win32, Microsoft will not thunk between them anymore.

So, if your trying to control a 64 bit browser (what will be downloaded by default from firefox) from a x32 bit python, it will exit before you can connect.. go and install a win32 version of firefox for the magic to happen

0

After trying almost all of the answers on different forums, a simple self trial resolved the problem and i.e. you need to have python, firefox browser and geckodriver in either 62 bit or 32 bit. Mismatch in this caused the problem in my case.

After ensuring that you are using the same bit version for all the 3 components, just use following lines to run firefox:

ffPath = "C:\\Drivers\\geckodriver.exe"
os.environ["webdriver.firefox.driver"] = ffPath
driver = webdriver.Firefox(executable_path=ffPath)
driver.get(url)
Rishab
  • 11
  • 2
0

The issue for me was the mis-match version between python and gekodriver . once all the involved party were 64 bit it worked like a charm

  • 1
    This would make a decent comment to OP's question ;) May be then OP can give you more information and you could come with a detailed answer. – DaveIdito Oct 08 '20 at 12:05
0

This error has troubled me a lot. Install this:

pip install -U selenium
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 14 '21 at 22:59
-1
binary = r'C:\Program Files\Mozilla Firefox\firefox.exe'
options = Options()
options.binary = binary
browser = webdriver.Firefox(firefox_options=options, executable_path=r"C:\Drivers\geckodriver.exe")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Lokesh
  • 1
  • 3
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Alessandro Mandelli Feb 12 '20 at 15:24