python - selenium 不适用于 Firefox 或 Chrome

标签 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 中。我似乎无法弄清楚...

最佳答案

对于 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')

关于python - selenium 不适用于 Firefox 或 Chrome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40437226/

相关文章:

javascript - mozrepl:遍历所有 firefox 窗口中的所有选项卡

python - 将可迭代列表传递给 itertools 函数

python - 使用 python mechanize 自动登录

amazon-web-services - 节点无法连接到Hub,继续发送注册事件

Python Selenium Firefox - 不安全连接错误警报

Selenium WebDriver - 从隐藏选择中获取选项

javascript - 定义全局变量 x 时让 Firebug 中断

css - 仅在 Firefox 中隐藏表格行的边框

python - 固定长度记录

Python 类型错误 : sort() takes no positional arguments