selenium不适用于Firefox或Chrome

时间:2016-11-05 10:36:59

标签: python selenium firefox

我正在尝试学习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。我似乎无法解决这个问题......

2 个答案:

答案 0 :(得分:7)

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

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

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

丁目: pip install selenium

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

答案 1 :(得分:0)

根据您留言中的一行

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()