Python 为什么我无法使用selenium下载zip文件?

Python 为什么我无法使用selenium下载zip文件?,python,html,selenium,Python,Html,Selenium,我正在尝试使用selenium,以便尝试从html网页下载测试文件。以下是我用作测试对象的完整html页面: <!DOCTYPE html> <html> <head> <title>Testpage</title> </head> <body> <div id="page"> Example Download link: <a href="testz

我正在尝试使用selenium,以便尝试从html网页下载测试文件。以下是我用作测试对象的完整html页面:

<!DOCTYPE html>
<html>
<head>
    <title>Testpage</title> 
</head>
<body>
    <div id="page">
    Example Download link: 
        <a href="testzip.zip">Download this testzip</a> 
    </div>
</body>
</html>
但是,如果我运行测试(例如,使用
nosetest
),浏览器正在打开,但之后什么也没有发生。没有错误消息,也没有下载,只是看起来“挂起”


你知道如何解决这个问题吗?

你不是在安装真正的web服务器。您只有一个html页面,但没有服务器来提供静态文件。您至少需要先安装一台服务器


但是,如果你的问题只是与下载文件有关,你可以使用一些国际网站进行测试。它会工作。

可能是因为文件url。使用普通http服务器托管站点,然后进行测试
python-msimplehttpserver
和test
http://localhost:8000/testweb.html
是的,谢谢,现在它可以工作了。但不幸的是,我还没有接近解决实际问题。因为对于不同的非公开网页,我有一个类似的下载链接,但在这里它不起作用…事实上,我有一个不同网页的问题,我想确保我的selenium代码可以工作。它在这种情况下可以工作,也可能与许多其他页面一起工作,但与我实际要测试的页面(包括zip文件中生成的数据)不起作用。下载没有开始…@Alex首先,请确保您要测试的站点是真实完整的站点。其次,在html页面中,文件的路径应该是
/testzip.zip
,但不是没有
/
的路径。你用来下载的代码没有问题。我刚在浏览器上看到一个弹出窗口,里面有一个真实的网页,问我在哪里下载文件。我想我已经关闭了这个弹出窗口…@Alex没有一个真实的例子,我不能说更多。你问题中给出的例子绝对不是一个网站,而是一个html页面。是的,我理解你。我得自己找别的办法来解决这个问题。不知道该怎么做,但我必须这么做。无论如何谢谢你。。。
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.dir", "/tmp")
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False )
profile.set_preference("pdfjs.disabled", True )                                       profile.set_preference("browser.helperApps.neverAsk.saveToDisk","application/zip")
profile.set_preference("plugin.disable_full_page_plugin_for_types", "application/zip")

browser = webdriver.Firefox(profile)
browser.get('file:///home/path/to/html/testweb.html')
browser.find_element_by_xpath('//a[contains(text(), "Download this testzip")]').click()