Python2.7+Selenium调用Firefox遇异常,多方案尝试仍未解决
I'm trying to get Selenium to work with Firefox using Python 2.7, but I've hit a bunch of roadblocks that I can't work through on my own. Let me walk through what I've tried so far:
First, I started with the most basic code to launch the browser:
from selenium import webdriver driver = webdriver.Firefox()
Right away, I got this error:
selenium.common.exceptions.WebDriverException: Message: 'firefox' executable needs to be in PATH.
I tried two common fixes I found, but neither worked:
- Specifying FirefoxBinary directly:
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary binary = FirefoxBinary('/usr/bin/firefox') browser = webdriver.Firefox(firefox_binary=binary) - Configuring via DesiredCapabilities:
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities caps = DesiredCapabilities.FIREFOX caps['marionette'] = True caps['binary'] = '/usr/bin/firefox' d = webdriver.Firefox(capabilities=caps)
I managed to get Firefox to launch once I modified the DesiredCapabilities code to include executable_path and closed all background Firefox processes:
d = webdriver.Firefox(capabilities=caps, executable_path='/usr/bin/firefox')
But now there's a new problem: when I run d.get("https://www.google.com"), the browser just stays on the Linux home page and won't navigate to the specified URL.
A few more details about my setup:
- Running
whereis firefoxin the terminal returns/usr/bin/firefox - I downloaded geckodriver from GitHub and placed it in the same folder as my script. I also tried pointing directly to it with:
But that didn't fix the original launch issue either.browser = webdriver.Firefox(executable_path="geckodriver")
I'm stuck here—any help figuring out how to get Firefox to both launch properly and navigate to URLs would be much appreciated!
内容的提问来源于stack exchange,提问作者Amjasd Masdhash

