0

This minimal example works just fine using standard python3 but it will not run in PyCharm project.

from selenium import webdriver
browser = webdriver.Firefox(executable_path="PATHTODRIVER")
browser.get('https://www.google.com')

The error shown in PyCharm is:

selenium.common.exceptions.SessionNotCreatedException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line

so it is not finding binary location, which I have tried specifying.

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.binary_location = "/usr/bin/firefox"
browser = webdriver.Firefox(firefox_options=options, executable_path="PATHTODRIVER")
browser.get('https://www.google.com')

The new error was

selenium.common.exceptions.InvalidArgumentException: Message: binary is not a Firefox executable

I do not understand the error well, so I stopped digging further. The code simply runs well using

python3 test.py

why it doesn't work under PyCharm project environment?

Seung
  • 763
  • 1
  • 7
  • 19
  • 1. have you moved the geckodriver to the usr/local/bin/? I take it you have but I'm just checking. If you haven't type mv ~/Downloads/geckodriver /usr/local/bin. 2. Have you tried taking out the path to driver and just having webdriver.Firefox()? – Insula Jan 19 '21 at 12:21
  • @Insula No I have kept it in project folder and I did not want to add PATH or move geckodriver to PATH directory. Since it is not included in PATH the code does not run without path to the driver – Seung Jan 19 '21 at 12:31
  • I suggest moving the geckodriver to the user local bin, it saves a lot of hassle – Insula Jan 19 '21 at 12:36
  • @Insula you may be right, but given how the code works just fine without PyCharm that should not be an issue. Also geckodriver is correctly loaded as it is complaining about browser location – Seung Jan 19 '21 at 12:48
  • hm, i see. I found this [tutorial](https://intellij-support.jetbrains.com/hc/en-us/community/posts/360009391540-PyCharm-can-t-see-python-binaries-even-if-they-are-there-) what could help you. – Insula Jan 19 '21 at 12:57

2 Answers2

0

This error message...

selenium.common.exceptions.SessionNotCreatedException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line

...implies that the GeckoDriver was unable to locate the binary/executable at the default location and you haven't supplied the moz:firefoxOptions.binary capability as well.


Solution

Possibly within your system Firefox is installed at a custom location and in these cases you need to pass the absolute path of the Firefox binary through the moz:firefoxOptions.binary capability as follows:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.binary_location = '/usr/bin/firefox'
driver = webdriver.Firefox(executable_path=PATHTODRIVER, firefox_options=options)
driver.get('http://google.com/')

You can find a relevant detailed discussion in Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided using GeckoDriver


The second error message...

selenium.common.exceptions.InvalidArgumentException: Message: binary is not a Firefox executable

...implies that the binary file which you have passed as binary_location attribute isn't a valid executable.

You need to pass the absolute location of the Firefox executable whereever it is located in your system.

You can find a relevant detailed discussion in InvalidArgumentException: Message: binary is not a Firefox executable error using GeckoDriver Firefox Selenium and Python

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

A similar thing happened to me. Hopefully you were able to solve the issue, but if not here's what helped me.

Since I'm using Pop_Os, PyCharm was installed as a FlatPack. I wasn't really sold on that idea, so I uninstalled PyCharm to reinstall it using the official JetBrains Toolbox (https://www.jetbrains.com/toolbox-app/).

Firefox and Geckodriver are installed/copied to system wide paths. My PATH variable is setup to see them just fine:

  • /usb/bin/firefox
  • /usr/bin/geckodriver

Also check your geckodriver.log file. Mine sits in the PyCharm project directory. This log file reported this error: Error: no DISPLAY environment variable specified.

It can be fixed by adding environment variables to PyCharm. I just enabled the checkbox to Include system environment variables and it magically worked. PyCharm Environment Variables Screenshot

tonschi
  • 1
  • 1