0

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>

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()

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?

Alex
  • 41,580
  • 88
  • 260
  • 469
  • It may be because of the file url. Host the site using a normal http server and then test. `python -m SimpleHTTPServer` and test `http://localhost:8000/testweb.html` – Tarun Lalwani Sep 18 '17 at 08:09
  • Yes thanks, now it works. But unfortunately I am no closer to solve the actual problem. Because for a different non-public webpage I have a similar download link and here it does not work ... – Alex Sep 18 '17 at 08:12

1 Answers1

1

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.

Sraw
  • 18,892
  • 11
  • 54
  • 87
  • I have, in fact, a problem with a different webpage and wanted to make sure my selenium code works. It works in this case and maybe with many other pages, but not with the page I am actually about to test (including the generated data in the zip file). The download does not start... – Alex Sep 18 '17 at 08:18
  • @Alex First, make sure the site you want to test is a real & complete one. Second, in your html page, the path of file should like `/testzip.zip` but not something without `/`. The code you use to download has no problem. – Sraw Sep 18 '17 at 08:23
  • I just got a popup on the browser with the real webpage asking me where to download the file. I thought I have deactivated this popup... – Alex Sep 18 '17 at 08:24
  • @Alex Without a real example, I can't tell more. The example given in your question is absolutely not a *website* but just a *html page*. – Sraw Sep 18 '17 at 08:28
  • Yes I understand you. I have to find some other way to solve this problem myself. No idea how to do that, but I HAVE TO. Thanks anyway... – Alex Sep 18 '17 at 08:32