为什么我无法使用硒下载压缩文件? [英] Why am I unable to download a zip file with selenium?

查看:108
本文介绍了为什么我无法使用硒下载压缩文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用硒以尝试从html网页下载测试文件。以下是我一直用作测试对象的完整html页面:

I am trying to use selenium in order to try to download a testfile from a html webpage. Here is the complete html page I have been using as test object:

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

which I put in the current working directory, along with some example zip file renamed to testzip.zip.

我的硒代码如下所示:

My selenium code looks as follows:

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 ),浏览器正在打开,但之后没有任何反应。没有错误信息,也没有下载,它似乎'挂'。

However, if I run the test (with nosetest for example), a browser is being opened, but after that nothing happens. No error message and no download, it just seems to 'hang'.

任何关于如何解决这个问题的想法?

Any idea on how to fix this?

推荐答案

没有建立一个真正的网络服务器。你只是有一个HTML页面,但不是服务器来提供静态文件。您至少需要先设置服务器。

You are not setting up a real web server. You just have a html page but not a server to serve static files. You need to at least setup a server first.

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

But if your question is just related to download files, you can just use some international web site to test. It will work.

这篇关于为什么我无法使用硒下载压缩文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆