无法运行webdriver.selenium webdriver的Firefox

时间:2018-08-07 17:43:07

标签: python selenium selenium-webdriver webdriver

我在使用硒时遇到问题

我在工作场所的Windows 7计算机上使用的是Python 3.6。 Firefox是61.0.1(64位) Python已加载到C:\Program Files (x86)\Python36-32

我的工作在H:\PythonPrograms

我已经成功安装了硒: C:\ Windows \ System32> pip install selenium Requirement already satisfied: selenium in c:\program files (x86)\python36-32\lib\site-packages (3.13.0)

根据此处找到的其他建议,我下载了geckodriver.exe并将其放置在C:\Program Files (x86)\Python36-32H:\PythonPrograms中。

我的路径包括:C:\Program Files (x86)\Python36-32\Scripts\;C:\Program Files (x86)\Python36-32\;H:\PythonPrograms

我正在按照“用Python自动完成无聊的东西”(Al Sweigart)中的步骤进行操作。 257

>>> from selenium import webdriver
>>> browser = webdriver.Firefox()
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    browser = webdriver.Firefox()
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 159, in __init__
    log_path=log_path)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\firefox\service.py", line 44, in __init__
    log_file = open(log_path, "a+") if log_path is not None and log_path != "" else None
PermissionError: [Errno 13] Permission denied: 'geckodriver.log'


>>> browser = webdriver.Firefox(executable_path=r'H:\PythonPrograms')
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    browser = webdriver.Firefox(executable_path=r'H:\PythonPrograms')
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 159, in __init__
    log_path=log_path)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\firefox\service.py", line 44, in __init__
    log_file = open(log_path, "a+") if log_path is not None and log_path != "" else None
PermissionError: [Errno 13] Permission denied: 'geckodriver.log'
>>> 

(这不是geckodriver.log上的权限。当我删除该文件并重试时,甚至会发生这种情况。我还检查了权限:)

当我在新的Python会话中以管理员身份运行Python时:

>>> from selenium import webdriver
>>> browser = webdriver.Firefox(executable_path=r'H:\PythonPrograms')
Traceback (most recent call last):
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
    stdin=PIPE)
  File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 997, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

在处理上述异常期间,发生了另一个异常:

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    browser = webdriver.Firefox(executable_path=r'H:\PythonPrograms')
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 160, in __init__
    self.service.start()
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\common\service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'PythonPrograms' executable needs to be in PATH.

我不了解正常运行Python与以管理员身份运行之间发生了什么。无论哪种方式,都无法给我预期的输出。


通过更改可执行文件路径以包括程序名称并删除“ r”限定符来添加新的运行示例。这是新结果:

>>> from selenium import webdriver
>>> browser = webdriver.Firefox(executable_path='H:\PythonPrograms\geckodriver.exe')
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    browser = webdriver.Firefox(executable_path='H:\PythonPrograms\geckodriver.exe')
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 170, in __init__
    keep_alive=True)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 156, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 251, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
    self.error_handler.check_response(response)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities

>>> 

1 个答案:

答案 0 :(得分:0)

与其使用正则表达式,不如尝试使用exe文件名给出确切的路径

browser = webdriver.Firefox(executable_path='H:\PythonPrograms\yourexenamehere.exe')