-1

I am writing python code to automate testing to download files. I have downloaded the file by clicking the download icon. What I am trying now is to assert that correct file was downloaded in the background. For this purpose, I need to extract the get request and parameters within. This parameters and URL will contain values to identify if/whether correct document was downloaded.

Can you please help me. Many thanks.

Below is the code I am running.

import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

mime_types = "application/pdf,application/vnd.adobe.xfdf,application/vnd.fdf,application/vnd.adobe.xdp+xml"

fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.manager.showWhenStarting", False)
fp.set_preference("browser.download.dir", os.getcwd())
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", mime_types)
fp.set_preference("plugin.disable_full_page_plugin_for_types", mime_types)
fp.set_preference("pdfjs.disabled", True)


driver = webdriver.Firefox(firefox_profile=fp)
driver.get('site-name')

# login
driver.find_element_by_class_name('textboxUserName').send_keys(name)
driver.find_element_by_id('dummy1').click()
driver.find_element_by_id('content__login_Password').send_keys(pwd)
driver.find_element_by_id('content__login_Password').send_keys(Keys.RETURN)

# search
driver.find_element_by_id('top-search-input').send_keys('9001')
driver.find_element_by_id('top-search-input').send_keys(Keys.RETURN)

# download
driver.find_element_by_xpath("//ul[@class='search-result-list']/li[1]/div/ul/li[7]").click()

As stated earlier, I want to make sure that the file has been downloaded. So I want to get certain values from GET when the button to download is clicked.

thanks

2
  • Where is your current code that does the downloading? Commented Jun 8, 2016 at 10:06
  • if you want the url, then why not instead of clicking on the download button, you simply fetch the associated link. If this isn't helpful please provide a little bit more information. Commented Jun 8, 2016 at 11:18

1 Answer 1

0

I see two ways you can do that.

  1. Like I mentioned in the comment, you could simply check the 'href' attribute of the download link, instead of clicking it.

  2. Once the program has clicked the link, use:

    driver.implicitly_wait(5) #just to be on the safe side

    print driver.current_url

Hope this helps.

UPDATE: Based on your comment, if the AJAX ends up opening another window, then this will do the trick:

for handle in driver.window_handles:
    driver.switch_to_window(handle)
    print driver.current_url
Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately there is no href attribute. It is implemented by Ajax i believe. Also, the code bypasses the native browser save dialogue for downloading file and instead saves it in a predefined location.
Try this then. After around an hour of research this is the best I could find. capture AJAX

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.