【问题标题】:selenium wont work with Firefox or Chromeselenium 不适用于 Firefox 或 Chrome
【发布时间】:2017-03-19 03:31:00
【问题描述】:

我正在尝试学习 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")

将它添加到我的环境变量中的 PATH 中。这个我好像搞不懂……

【问题讨论】:

标签: python selenium firefox


【解决方案1】:

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

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

现在您可以按照anwser 中的说明将 geckodriver 添加到您的路径中。或者您可以直接在您的代码中进行设置,如下所示:

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

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

【讨论】:

  • 我能够让它与您的信息一起使用。谢谢。
  • 我尝试了很多不同的方法来安装 selenium,但仍然不断收到错误:FileNotFoundError Traceback (最近一次调用最后一次) ~\Anaconda3\lib\site-packages\selenium\webdriver\common\service .py in start(self) 75 stderr=self.log_file, ---> 76 stdin=PIPE) 77 除了 TypeError:
【解决方案2】:

根据您消息中的行

selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

您没有 geckodriver.exe。需要从enter link description here下载,将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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 2010-11-08
    • 2013-05-03
    • 2016-09-25
    相关资源
    最近更新 更多