Selenium不能与Firefox或Chrome一起使用。

9

我正在尝试学习Python网络爬虫,但无法让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")

将它添加到我的环境变量路径中。我似乎无法弄清楚这一点...


1
看起来是重复的问题,类似于https://dev59.com/hlkS5IYBdhLWcg3wNkH6 - saurabh baid
2个回答

13

现在,无论是 Firefox 还是 Chrome,您都需要下载 geckodriver / chromedriver。这些驱动程序对于在您安装的浏览器和 Selenium 之间进行通信是必要的。因此,您需要:

  • 为 Python 安装 Selenium(pip install selenium
  • 下载您想要使用的浏览器的驱动程序(chromedriver、geckodriver、operadriver 等)
  • 在您的系统上安装要使用的浏览器(可能已经有了)

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

Chome: driver = webdriver.Chrome(executable_path='/path/to/chromedriver.exe')

Firefox: driver = webdriver.Firefox(executable_path='/opt/geckoDriver/geckodriver.exe')


我已经根据您提供的信息使其正常工作了。谢谢。 - AutomateMyJob
我尝试了很多不同的方法来安装Selenium,但仍然一直出现错误:FileNotFoundError Traceback (most recent call last) ~\Anaconda3\lib\site-packages\selenium\webdriver\common\service.py in start(self) 75 stderr=self.log_file, ---> 76 stdin=PIPE) 77 except TypeError: - Steve Gon

0
根据您消息中的行。
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

你没有geckodriver.exe。你需要从这里输入链接描述下载它,将exe放在Python脚本所在的目录中,然后尝试下面的代码:

请尝试此代码:

# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

gecko = os.path.normpath(os.path.join(os.path.dirname(__file__), 'geckodriver'))
binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe')
browser = webdriver.Firefox(firefox_binary=binary,executable_path=gecko+'.exe')
browser.get('http:///www.google.com')
browser.close()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接