【问题标题】:Webpage not opening in Firefox - Selenium Python网页无法在 Firefox 中打开 - Selenium Python
【发布时间】:2017-08-02 15:08:06
【问题描述】:

我正在尝试使用 Selenium 远程运行网页,然后触发按钮单击它。我能够成功打开 Firefox,但网页没有加载,并且 Firefox 会在一段时间后自动关闭。 (尝试了 google.com 和其他页面作为测试,也没有加载)。谁能建议在这里做什么?

操作系统 - Ubuntu 14.04.1

Python - 2.7.6

硒 - 3.3.0

火狐 - 39.0.3

这是我的python代码

import urllib, urllib2, cookielib
from contextlib import closing
from selenium import webdriver
from selenium.webdriver import Firefox # pip install selenium
from selenium.webdriver.support.ui import WebDriverWait

with closing(Firefox(executable_path="/usr/bin/firefox")) as driver:
    driver.implicitly_wait(10)
    driver.get("https://www.google.com")
    #driver.get("http://wsb.com/Assingment2/expcase16.php")
    button = driver.find_element_by_id('send')
    button.click()
    target_window = driver.window_handles[1]
    driver.switch_to_window(target_window)
    exploit_page_content = driver.page_source
    print "Exploit successful \n" + exploit_page_content

【问题讨论】:

  • 您是否在控制台上看到任何错误消息?

标签: python selenium firefox selenium-webdriver selenium-firefoxdriver


【解决方案1】:

我怀疑 selenium 正在尝试使用 geckodriver,因为这是默认设置。但它仅受 Firefox 48 版的支持。 See Jim's answer to a different question for more info。尝试使用旧版 Firefox 驱动程序,如下所示:

driver = Firefox(executable_path="/usr/bin/firefox", capabilities= {"marionette": False })

【讨论】:

  • 非常感谢升级到最新的 Firefox 解决了这个问题,您的建议也适用于旧的 Firefox 版本。
  • 太好了。如果您或其他任何人有兴趣,我添加了一个指向另一个问题的答案的链接,该链接提供了有关驱动程序与不同版本 Firefox 的兼容性的更多信息。
【解决方案2】:

您似乎正在使用conextlib.closing(),并且根据文档,您基本上是在对象上调用close() 方法:

from contextlib import contextmanager

@contextmanager
def closing(thing):
    try:
        yield thing
    finally:
        thing.close()

根据 selenium 文档:

driver.close() – 关闭设置焦点的浏览器窗口。

就建议而言,这取决于您想做什么。如果您想继续处理网页,那么显然扩展您的代码。或者删除上下文并在完成后显式调用driver.close()。同样,这取决于您的任务是什么。

【讨论】:

  • 您好,先生,感谢您的回复。我尝试删除 close() 并在最后明确添加 driver.close() 。我仍然面临同样的问题。 driver = Firefox(executable_path="/usr/bin/firefox") driver.implicitly_wait(3) driver.get("https://www.google.com") driver.close()
  • 我猜你也可以设置页面加载超时:*.com/questions/32276654/…
最近更新 更多