selenium不适用于Firefox或Chrome
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了selenium不适用于Firefox或Chrome相关的知识,希望对你有一定的参考价值。
我正在尝试学习python web scraping,但我不能让selenium与任何一个浏览器一起工作。
from selenium import webdriver
browser = webdriver.Firefox()
这是我的所有代码,我得到了一个错误。
Traceback (most recent call last):
File "C:Users jhallAppDataLocalProgramsPythonPython35-32libsite-packagesseleniumwebdrivercommonservice.py", line 64, in start
stdout=self.log_file, stderr=self.log_file)
File "C:Users jhallAppDataLocalProgramsPythonPython35-32libsubprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "C:Users jhallAppDataLocalProgramsPythonPython35-32libsubprocess.py", line 1220, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "H:codingpracticepythonpython challenge.com.py", line 2, in <module>
browser = webdriver.Firefox()
File "C:Users jhallAppDataLocalProgramsPythonPython35-32libsite-packagesseleniumwebdriverfirefoxwebdriver.py", line 135, in __init__
self.service.start()
File "C:Users jhallAppDataLocalProgramsPythonPython35-32libsite-packagesseleniumwebdrivercommonservice.py", line 71, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00A11350>>
Traceback (most recent call last):
File "C:Users jhallAppDataLocalProgramsPythonPython35-32libsite-packagesseleniumwebdrivercommonservice.py", line 163, in __del__
self.stop()
File "C:Users jhallAppDataLocalProgramsPythonPython35-32libsite-packagesseleniumwebdrivercommonservice.py", line 135, in stop
if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
我已经尝试了在互联网上找到的所有内容,即添加代码路径
from selenium import webdriver
browser = webdriver.Firefox("C:Program Files (x86)Mozilla Firefoxfirefox.exe")
在我的环境变量中添加PATH。我似乎无法弄清楚这一点......
答案
对于Firefox和Chrome,您现在需要下载geckodriver / chromedriver。这些驱动程序是您安装的浏览器和selenium之间进行通信所必需的。所以你需要:
- 为python安装selenium(
pip install selenium
) - 下载drivers为您要使用的浏览器(chromedriver,geckodriver,operadriver等)
- 安装要在系统上使用的浏览器(可能已经有了这个)
现在你可以将geckodriver添加到你的路径中,就像这个qazxsw poi中提到的那样。或者您可以直接在代码中进行设置,如下所示:
め:z zxswい
Firefox:anwser
另一答案
根据你邮件中的行
driver = webdriver.Chrome(executable_path='/path/to/chromedriver.exe')
你没有geckodriver.exe。您需要从driver = webdriver.Firefox(executable_path='/opt/geckoDriver/geckodriver.exe')
下载它,将exe放在Python脚本所在的目录中并尝试以下代码:
请尝试以下代码:
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
以上是关于selenium不适用于Firefox或Chrome的主要内容,如果未能解决你的问题,请参考以下文章
Selenium C#FirefoxDriver不适用于最新的Selenium和Firefox