如何解决Python-Selenium错误

2024-10-02 00:20:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用selenium来处理一些使用JavaScript代码的网站。首先,我使用下面的简单示例

from selenium import webdriver

browser = webdriver.Firefox()

browser.get('http://www.yahoo.com')

browser.quit()

但这根本不管用。错误消息如下所示。我想我可能错过了硒元素或其他元素。你能给我拿点光吗?

^{pr2}$

我检查了"geckodriver.log",它显示了以下错误消息:

1479623778556   geckodriver INFO    Listening on 127.0.0.1:49723
1479623778625   mozprofile::profile INFO    Using profile path /tmp/rust_mozprofile.WMDGNONHQTud
1479623778628   geckodriver::marionette INFO    Starting browser /usr/bin/firefox
1479623778641   geckodriver::marionette INFO    Connecting to Marionette on localhost:35691
No protocol specified
Unable to init server: Could not connect: Connection refused
Error: cannot open display: :0

Tags: to代码browserinfo消息元素onselenium
3条回答

随着Selenium 3.0的发布,Firefox Selenium驱动程序已经更新为一个名为Marionette或“geckodriver”的新驱动程序。这是系统上的一个单独的二进制文件。在

了解如何将geckodriver与Python一起使用:Selenium install Marionette webdriver

如果要使用旧的Selenium驱动程序,版本2.x:

现在Firefox在Selenium web驱动程序方面做得不太好,我建议您改用Chrome驱动程序

Download the chrome driver and extract the file. set the chrome driver path in the Environment path and then try with same code by replacing Firefox() to Chrome() or follow below code....

from selenium import webdriver

Path = ("c://chromedriver//chromedriver.exe")
browser = webdriver.Chrome(path)
#eliminate above two steps if the path is set in Environment variable 

browser = webdriver.Chrome() 

browser.get('http://www.yahoo.com')

browser.quit()

Pl检查路径或者如果您在Linux上,请查看http://www.linuxquestions.org/questions/linux-server-73/can't-open-display-no-protocol-specified-using-gdm-4175462031/

相关问题 更多 >

    热门问题