Selenium WebDriver:Firefox启动,但未打开URL [英] Selenium WebDriver: Firefox starts, but does not open the URL

查看:104
本文介绍了Selenium WebDriver:Firefox启动,但未打开URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次安装Selenium,但入门上遇到困难.

I've just installed Selenium for the first time, and I'm having trouble to get started.

使用pip可以很好地进行安装:

Installation went fine with pip:

pip install selenium

而且我可以在Python中import selenium.

And I can import selenium within Python.

现在,我正在尝试运行以下示例脚本:

Now I'm trying to run the following sample script:

from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title

发生的情况是Firefox打开,但没有导航到" http://www.python.org "(类似于在此问题中描述的行为-它仅显示空白页)

What happens is that Firefox opens, but it does not navigate to "http://www.python.org" (similar to the behaviour described in this question - it only shows a blank page)

大约60秒钟没有任何反应,直到引发以下异常:

For about 60 seconds nothing happens, until the following exception raised:

Traceback (most recent call last):
  File "selenium-test.py", line 4, in <module>
    driver = webdriver.Firefox()
  File "/home/usr1/.local/lib/python2.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 61, in __init__
    desired_capabilities=capabilities)
  File "/home/usr1/.local/lib/python2.6/site-packages/selenium/webdriver/remote/webdriver.py", line 72, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/home/usr1/.local/lib/python2.6/site-packages/selenium/webdriver/remote/webdriver.py", line 114, in start_session
    'desiredCapabilities': desired_capabilities,
  File "/home/usr1/.local/lib/python2.6/site-packages/selenium/webdriver/remote/webdriver.py", line 165, in execute
    self.error_handler.check_response(response)
  File "/home/usr1/.local/lib/python2.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 136, in check_response
    raise exception_class(value)
selenium.common.exceptions.WebDriverException: Message: u'<HTML><HEAD>\r\n<TITLE>Network Error</TITLE>\r\n</HEAD>\r\n<BODY>\r\n<FONT face="Helvetica">\r\n<big><strong></strong></big><BR>\r\n</FONT>\r\n<blockquote>\r\n<TABLE border=0 cellPadding=1 width="80%">\r\n<TR><TD>\r\n<FONT face="Helvetica">\r\n<big>Network Error (tcp_error)</big>\r\n<BR>\r\n<BR>\r\n</FONT>\r\n</TD></TR>\r\n<TR><TD>\r\n<FONT face="Helvetica">\r\nA communication error occurred: "Operation timed out"\r\n</FONT>\r\n</TD></TR>\r\n<TR><TD>\r\n<FONT face="Helvetica">\r\nThe Web Server may be down, too busy, or experiencing other problems preventing it from responding to requests. You may wish to try again at a later time.\r\n</FONT>\r\n</TD></TR>\r\n<TR><TD>\r\n<FONT face="Helvetica" SIZE=2>\r\n<BR>\r\nFor assistance, contact your network support team.\r\n</FONT>\r\n</TD></TR>\r\n</TABLE>\r\n</blockquote>\r\n</FONT>\r\n</BODY></HTML>'

这些是软件版本

  • Firefox ESR 17.0.5
  • 硒(Python绑定)2.35.0
  • Python 2.6.6
  • Red Had Linux 6.3
  • 已安装"Firefox WebDriver 2.35.0"浏览器扩展

推荐答案

好吧,在搜索了一会儿之后,我注意到通常该问题是Selenium中的错误(可能,但不太可能)或代理问题. 尽管如此,没有任何关于如何解决代理问题的答案似乎起作用.

Ok, after searching around for a while I noticed that usually the problem was a bug in Selenium (possible, but rather unlikely), or a proxy issue. Still, none of the answers suggesting how to solve the proxy issue seemed to work.

最后我明白了:您需要在所有位置取消设置所有代理设置(环境变量,在我的情况下,这是Gnome上的问题).稍后在创建网络驱动程序时,您需要传递一个配置文件,该配置文件将浏览器代理设置设置为您实际使用的设置(在我的情况下为自动配置网址)

Finally I got it: you need to unset all proxy settings everywhere (environment variables, and - in my case this was the issue- on Gnome). Later when you create the webdriver, you need to pass a profile which sets the browser proxy settings to what you actually use (in my case an automatic config url)

1)取消设置http_proxy环境变量(由urllib使用)

1) Unset the http_proxy environment variable (which is used by urllib)

export http_proxy=

2):已清除Gnome代理设置: 系统->首选项->网络代理->选择直接Internet连接"

2) Cleared Gnome proxy settings: System --> Preferences --> Network Proxy --> Select "Direct Internet Connection"

3)webdriver.Firefox()开始,该配置文件用于配置代理(在这种情况下,这是自动代理配置)

3) Started webdriver.Firefox() with a profile which configures the proxy (in this case it's an automatic proxy configuration)

fp = webdriver.FirefoxProfile()
# Here "2" stands for "Automatic Proxy Configuration"
fp.set_preference("network.proxy.type", 2)
fp.set_preference("network.proxy.autoconfig_url",
                  "http://proxy-address-here:8080/") 
driver = webdriver.Firefox(firefox_profile=fp)

这篇关于Selenium WebDriver:Firefox启动,但未打开URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆