简体   繁体   English

启动Firefox时出现python webdriver错误

[英]python webdriver error when starting Firefox

I recently downloaded selenium and tried to run this script: 我最近下载了selenium并尝试运行此脚本:

 from selenium import webdriver
 driver = webdriver.Firefox()

but I get this error: 但我得到这个错误:

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    driver = webdriver.Firefox()
  File "C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\site-packages\selenium-3.0.1-py2.7.egg\selenium\webdriver\firefox\webdriver.py", line 145, in __init__
    keep_alive=True)
  File "C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\site-packages\selenium-3.0.1-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 92, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\site-packages\selenium-3.0.1-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 179, in start_session
    response = self.execute(Command.NEW_SESSION, capabilities)
  File "C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\site-packages\selenium-3.0.1-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\site-packages\selenium-3.0.1-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
WebDriverException: Message: Expected browser binary location, but unable to find binary in default location, no 'firefox_binary' capability provided, and no binary flag set on the command line

Oh by the way this opens my geckodriver.exe before it prints the error 哦顺便说一下,在打印错误之前打开我的geckodriver.exe

I got around this by manually setting the binary location as per the following answer: 我通过按照以下答案手动设置二进制位置来解决这个问题:

https://stackoverflow.com/a/25715497 https://stackoverflow.com/a/25715497

Remember to set your binary to the actual location of the firefox binary on your computer 请记住将二进制文件设置为计算机上firefox二进制文件的实际位置

eg: 例如:

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe')
self.browser = webdriver.Firefox(firefox_binary=binary)

(note: the 'r' before the first quote in the file path string makes python see the string as 'raw' text, and therefore you don't need to escape characters like '\\' - you can just put in the path as it actually is) (注意:文件路径字符串中第一个引号之前的'r'使得python将字符串视为'raw'文本,因此您不需要像'''那样转义字符 - 您只需将路径放入它实际上是)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM