[英]cannot run webdriver.Firefox from selenium 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-32
和H:\\PythonPrograms
。
我的路徑包括: C:\\Program Files (x86)\\Python36-32\\Scripts\\;C:\\Program Files (x86)\\Python36-32\\;H:\\PythonPrograms
我正在按照“使用Python自動完成無聊的事情”(Al Sweigart)p中的步驟進行操作。 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
>>>
而不是使用正則表達式,請嘗試使用exe文件名提供確切的路徑
browser = webdriver.Firefox(executable_path='H:\PythonPrograms\yourexenamehere.exe')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.