Python Selenium WebDriver:Firefox 启动,但不打开 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18768658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:46:55  来源:igfitidea点击:

Selenium WebDriver: Firefox starts, but does not open the URL

pythonseleniumselenium-webdriver

提问by E.Z.

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

我刚刚第一次安装了 Selenium,我很难开始。

Installation went fine with pip:

安装顺利pip

pip install selenium

And I can import seleniumwithin Python.

我可以import selenium在 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

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)

发生的情况是 Firefox 打开,但它没有导航到“ http://www.python.org”(类似于这个问题中描述的行为- 它只显示一个空白页面)

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

大约 60 秒没有任何反应,直到出现以下异常:

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>'

These are the software versions

这些是软件版本

  • Firefox ESR 17.0.5
  • Selenium (Python bindings) 2.35.0
  • Python 2.6.6
  • Red Had Linux 6.3
  • the "Firefox WebDriver 2.35.0" browser extension is installed
  • 火狐 ESR 17.0.5
  • 硒(Python 绑定)2.35.0
  • 蟒蛇 2.6.6
  • Red Had Linux 6.3
  • 安装了“Firefox WebDriver 2.35.0”浏览器扩展

采纳答案by E.Z.

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.

好的,在搜索了一段时间后,我注意到问题通常是 Selenium 中的错误(可能,但不太可能),或者是代理问题。尽管如此,建议如何解决代理问题的答案似乎都不起作用。

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)

最后我明白了:您需要在任何地方取消所有代理设置(环境变量,并且 - 在我的情况下,这是 Gnome 上的问题)。稍后当您创建 webdriver 时,您需要传递一个配置文件,该配置文件将浏览器代理设置设置为您实际使用的内容(在我的情况下是一个自动配置 url)

1)Unset the http_proxyenvironment variable (which is used by urllib)

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

export http_proxy=

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

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

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

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

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)

回答by Dharma Krish

Needs to upgrade the selenium, If you are using Latest version of Firefox, you should use latest version of selenium

需要升级 selenium,如果您使用的是最新版本的 Firefox,则应使用最新版本的 selenium

For Python, Enter this command

对于 Python,输入此命令

pip install -U selenium

For Java, Remove the old jar and Download Latest Version from here http://www.seleniumhq.org/download/and Attach into build path. It will work find . Happy Testing with Firefox

对于 Java,请从http://www.seleniumhq.org/download/删除旧 jar 并下载最新版本,然后附加到构建路径中。它会工作 find 。使用 Firefox 进行愉快的测试

回答by ManSunday

Please also try turning off your localhost(127.0.0.1) web server if you have any running on the usual port 80.

如果您在常用端口 80 上运行任何服务器,请尝试关闭您的 localhost(127.0.0.1) Web 服务器。

The Firefox Binary does not allow you to load the profile if there is a localhost server running.

如果本地主机服务器正在运行,Firefox 二进制文件将不允许您加载配置文件。

See line 81 in selenium\webdriver\firefox\firefox_binary.py which points to the function / method is_connectable(self)

请参阅 selenium\webdriver\firefox\firefox_binary.py 中的第 81 行,它指向函数/方法 is_connectable(self)

def is_connectable(self):

    """Trys to connect to the extension but do not retrieve context."""
    try:
        socket_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        socket_.settimeout(1)
        socket_.connect(("127.0.0.1", self.profile.port))
        socket_.close()
        return True
    except socket.error:
        return False

GLHF

GLHF