#!/usr/bin/env python
# -*- coding: utf8 -*-
from selenium import webdriver
browser = webdriver.Firefox('d:\\1\\geckodriver')
browser.get('http://seleniumhq.org/')
使用 selenium 时出错。我正在尝试开始使用这个库。在 PATH 变量中注册的已安装 geckodriver 出现错误:
回溯(最后一次调用):文件“D:\proba1.py”,第 6 行,在浏览器中 = webdriver.Firefox('d:\1\geckodriver') 文件“C:\Users\Sylwia.thonny\BundledPython36\ lib\site-packages\selenium\webdriver\firefox\webdriver.py”,第 164 行,在init self.service.start() 文件“C:\Users\Sylwia.thonny\BundledPython36\lib\site-packages\selenium\ webdriver\common\service.py",第 104 行,在 start raise WebDriverException("Can not connect to the Service %s" % self.path) selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service geckodriver
有人猜测问题出在映射 127.0.0.1 和 localhost,这里讨论了类似的问题:
https://stackoverflow.com/questions/45843575/python3selenium-raise-message-can-not-connect-to-the-service-geckodriver
https://github.com/SeleniumHQ/selenium/issues/2903
https://github.com/mozilla/geckodriver/issues/270
所有目击者都表示,必须通过计算机上的主机或通过 DNS 解析器设置正确映射 127.0.0.1 和 localhost。
由于您使用的是 Windows,请尝试在 C:\Windows\System32\drivers\etc\hosts 中查找文件末尾的“127.0.0.1 localhost”行,默认情况下应将其注释掉。如果您已将其注释掉,请尝试取消注释以明确指定映射,重新启动并尝试运行 Selenium