selenium.common.exceptions.WebDriverException: Message: 'firefox' executable needs to be in PATH with GeckoDriver Firefox Selenium and Python

馋奶兔 提交于 2021-02-05 07:19:05

问题


I am trying to open Firefox with selenium,i tried

from selenium import webdriver
driver=webdriver.Firefox()

But i got the following error:

selenium.common.exceptions.WebDriverException: Message: 'firefox' executable needs to be in PATH.

Selenium using Python - Geckodriver executable needs to be in PATH

I tried

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary('/usr/bin/firefox')
browser = webdriver.Firefox(firefox_binary=binary)

Also tried

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.FIREFOX
caps['marionette'] = True
caps['binary'] = '/usr/bin/firefox'
d = webdriver.Firefox(capabilities=caps)

`but still did not work.

However, when i tried using the above code replacing the last line with

d=webdriver.Firefox(capabilities=caps,executable_path='/usr/bin/firefox') and having my Firefox closed from background it would open Firefox but I can't simply d.get("https://www.google.com") it gets stuck on Linux homepage and doesn't open anything.

After typing whereis firefox in terminal i got /usr/bin/firefox,also if it matters i use python 2.7

Note: I hope this isn't a duplicate of the above link because i tried the answers and it didn't fix it.

I installed geckodriver from github, and tried browser=webdriver.Firefox(executable_path="geckodriver") ,I have placed the driver is the same directory.


回答1:


It is still not clear why you are seeing the error as:

selenium.common.exceptions.WebDriverException: Message: 'firefox' executable needs to be in PATH.

In majority of the cases the common PATH related error is associated with geckodriver.

However, while working with Selenium 3.x you need to download the latest GeckoDriver from mozilla/geckodriver and save it anywhere in your system and provide the absolute path of the GeckoDriver through the argument executable_path.

The following code block works perfecto to open Firefox Nightly Browser (installed at customized location):

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.firefox.options import Options
    
    options = Options()
    options.binary_location = '/path/to/firefox'
    driver = webdriver.Firefox(firefox_options=options, executable_path='/path/to/geckodriver')
    driver.get('http://google.com/')
    print("Page title is: %s" %(driver.title))
    driver.quit()
    
  • Console Output:

    Page title is: Google
    


来源:https://stackoverflow.com/questions/54261423/selenium-common-exceptions-webdriverexception-message-firefox-executable-nee

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