selenium不适用于Firefox或Chrome

问题描述 投票:5回答:2

我正在尝试学习python web scraping,但我不能让selenium与任何一个浏览器一起工作。

from selenium import webdriver
browser = webdriver.Firefox()

这是我的所有代码,我得到了一个错误。

Traceback (most recent call last):
  File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\common\service.py", line 64, in start
    stdout=self.log_file, stderr=self.log_file)
  File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 950, in __init__
    restore_signals, start_new_session)
  File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\subprocess.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:\codingpractice\python\python challenge.com.py", line 2, in <module>
    browser = webdriver.Firefox()
  File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 135, in __init__
    self.service.start()
  File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\common\service.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\tjhall\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__
    self.stop()
  File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\common\service.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 Firefox\firefox.exe")

在我的环境变量中添加PATH。我似乎无法弄清楚这一点......

python selenium firefox
2个回答
7
投票

对于Firefox和Chrome,您现在需要下载geckodriver / chromedriver。这些驱动程序是您安装的浏览器和selenium之间进行通信所必需的。所以你需要:

  • 为python安装selenium(pip install selenium
  • 下载drivers为您要使用的浏览器(chromedriver,geckodriver,operadriver等)
  • 安装要在系统上使用的浏览器(可能已经有了这个)

现在你可以将geckodriver添加到你的路径中,就像这个qazxsw poi中提到的那样。或者您可以直接在代码中进行设置,如下所示:

め:z zxswい

Firefox:anwser


0
投票

根据你邮件中的行

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.
© www.soinside.com 2019 - 2024. All rights reserved.