imports...
browser = webdriver.Firefox()
browser.get('http://google.com')
我正在尝试执行上面的代码。它过去可以工作,现在不行了:Firefox甚至都不打开。代码、Python、Selenium和所有其他库、Firefox和geckodriver自工作以来没有任何变化。改变的是,我在Windows PC中安装了许多新的应用程序,例如:Plex媒体服务器、Eclipse、Java开发工具包和许多Java库、Maven、Postgresql和Git。
以下是我运行时终端中显示的所有内容:
Traceback (most recent call last):
File "C:UsershougyOneDriveDocumentosplijseleniumtest.py", line 26, in <module>
browser = webdriver.Firefox()
File "C:UsershougyAppDataLocalProgramsPythonPython310libsite-packagesseleniumwebdriverfirefoxwebdriver.py", line 175, in __init__
self.service.start()
File "C:UsershougyAppDataLocalProgramsPythonPython310libsite-packagesseleniumwebdrivercommonservice.py", line 108, in start
raise WebDriverException("Can not connect to the Service %s" % self.path)
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service geckodriver
当它工作时;1636932662336壁虎驾驶员信息收听127.0.0.1:49684";出现在geckodriver.log中,然后是很多内容。现在,只显示该消息。每次;geckodriver INFO";和端口不同。
我在想,也许问题是他们都试图使用localhost。所以我确定Plex和Postgre没有运行。我还尝试了一个解决方案,希望您确保127.0.0.1在Windows文件中映射到localhost。
我还尝试了许多常见的硒解决方案,比如添加一些选项,确保找到壁虎驱动器并更新所有内容。
如果我在家里的另一台电脑上运行相同的代码,它运行得很好,但我需要让它在这台电脑上工作。
我已经一年多没有需要使用Selenium了,我以前也使用Chrome和chromedriver,但我遇到了问题,不得不一直浪费大量时间进行调试,可能是因为Chrome更新频繁。所以最近,当我无法解决最新的问题时,我发现我可以使用Firefox来解决这个问题,并将其配置为从不更新。我以为我的痛苦已经过去了,但我又来了。这是我上次用chromedriver无法解决的问题:
以前工作的Selenium Python代码现在打开浏览器,然后被困在";数据:";。说";chrome not reach";
此错误消息。。。
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service geckodriver
意味着您的程序无法启动GeckoDriver服务。
解决方案
使用键executable_path
指向下载的GeckoDriver匹配版本,如下所示:
browser = webdriver.Firefox(executable_path=r'C:UtilityBrowserDriversgeckodriver.exe')
browser.get('http://google.com')
但是,您的主要问题似乎是您正在使用的二进制文件版本之间的不兼容。
解决方案
确保:
- GeckoDriver更新到当前GeckoDrive v0.30.0级别
- Firefox已更新到当前的Firefox 94.0.1版本。(根据geckodriver版本与Selenium和Firefox所需版本之间的映射(
- 如果您的基本Web客户端版本太旧,请将其卸载并安装最新的GA和发布的Web客户端版本
- 进行系统重新启动
- 以非root用户身份执行您的
@Test
- 总是调用CCD_ 4方法中的CCD_;优雅地销毁WebDriver和Web客户端实例
参考
您可以在中找到一些相关的详细讨论
- Python Selenium"无法连接到服务%s"%linux服务器中的self.path
- selenium.com.mon.exceptions.WebDriverException:消息:无法通过SeleniumPython连接到使用ChromeDriver Chrome的服务错误
- selenium.com.mon.exceptions.WebDriverException:消息:打开chrome浏览器时无法连接到服务chromedriver.exe