Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided using GeckoDriver

有些话、适合烂在心里 提交于 2021-02-13 17:30:54

问题


from selenium import webdriver;
browser= webdriver.Firefox();
browser.get('http://www.seleniumhq.org');

When I try to run this code, it gives me an error 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.

Any thoughts-highly appreciated!


回答1:


This error 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 find the Firefox binary at the default location. Additionally you haven't passed the moz:firefoxOptions.binary capability.


Solution

Possibly within your system firefox is installed in a custom location and 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 = r'C:\Program Files\Mozilla Firefox\firefox.exe'
driver = webdriver.Firefox(executable_path=r'C:\WebDrivers\geckodriver.exe', firefox_options=options)
driver.get('http://google.com/')

References

You can find a couple of relevant detailed discussion in:

  • SessionNotCreatedException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary'
  • InvalidArgumentException: Message: binary is not a Firefox executable error using GeckoDriver Firefox Selenium and Python
  • Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided



回答2:


You need to download geckodriver.

https://github.com/mozilla/geckodriver/releases

from selenium import webdriver;

browser= webdriver.Firefox('./geckodriver');
browser.get('http://www.seleniumhq.org');



回答3:


You should download appropriate web driver from https://github.com/mozilla/geckodriver/releases and put it into folder where your py file is. Also you can put it anywhere as long as the location of the file it is in your system path.




回答4:


Selenium uses a web driver (a specific one for each web browser) in order to communicate with the browser installed on your system (Firefox in your case).

To use Firefox, you have to:

  1. Download its web driver from https://github.com/mozilla/geckodriver/releases
  2. Put the web driver in a specific location in the file system (same folder as the python script for example)
  3. Add the web driver location path when initializing in the python code.

So the final code would look like this:

from selenium import webdriver

browser = webdriver.Firefox('./geckodriver')

browser.get('https://www.python.org/')

Note: Sometimes a newer version of the web driver isn't compatible with an older version of the browser installed on your system.



来源:https://stackoverflow.com/questions/65318382/expected-browser-binary-location-but-unable-to-find-binary-in-default-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!