如何解决Python Selenium中的错误

时间:2016-11-20 06:51:15

标签: javascript python selenium firefox

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

from selenium import webdriver

browser = webdriver.Firefox()

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

browser.quit()

虽然这甚至不起作用。错误消息如下所示。我想我可能会错过硒或其他东西。你能给我一些启示吗?

Traceback (most recent call last):
  File "/home/rnie/webdriver-tutorial-1x.py", line 3, in <module>
    browser = webdriver.Firefox()
  File "/usr/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py", line 145, in __init__
    keep_alive=True)
  File "/usr/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 92, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/usr/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 179, in start_session
    response = self.execute(Command.NEW_SESSION, capabilities)
  File "/usr/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "/usr/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refused

我检查了"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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

随着Selenium 3.0的发布,Firefox Selenium驱动程序已更新为名为Marionette或&#34; geckodriver&#34;的新驱动程序。这是您系统上的单独二进制文件。

了解如何在Python中使用geckodriver:Selenium install Marionette webdriver

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

答案 2 :(得分:-1)

目前Firefox对Selenium网络驱动程序效果不佳我可以建议您改用Chrome驱动程序

  

下载chrome驱动程序并解压缩文件。在环境路径中设置chrome驱动程序路径,然后将 Firefox()替换为Chrome(),尝试使用相同的代码   或按照以下代码....

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