网页无法在Firefox中打开 - Selenium Python

时间:2017-03-12 15:43:39

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

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

OS - Ubuntu 14.04.1

Python - 2.7.6

Selenium - 3.3.0

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

2 个答案:

答案 0 :(得分:3)

我怀疑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 })

答案 1 :(得分:0)

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

from contextlib import contextmanager

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

根据硒文件:

  

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

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

相关问题