【发布时间】:2017-09-18 07:42:44
【问题描述】:
我正在尝试使用 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>
我把它放在当前工作目录中,连同一些示例zip 文件重命名为testzip.zip。
我的 selenium 代码如下:
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()
但是,如果我运行测试(例如使用nosetest),浏览器正在打开,但之后什么也没有发生。没有错误消息,也没有下载,它似乎只是“挂起”。
知道如何解决这个问题吗?
【问题讨论】:
-
可能是文件url的原因。使用普通的 http 服务器托管站点,然后进行测试。
python -m SimpleHTTPServer和测试http://localhost:8000/testweb.html -
是的,谢谢,现在可以了。但不幸的是,我并没有更接近解决实际问题。因为对于不同的非公开网页,我有一个类似的下载链接,在这里它不起作用......