-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

Mustafa Salam
  • 150
  • 1
  • 2
  • 12

1 Answers1

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
Swakeert Jain
  • 776
  • 5
  • 16
  • 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. – Mustafa Salam Jun 08 '16 at 13:00
  • Try this then. After around an hour of research this is the best I could find. [capture AJAX](http://stackoverflow.com/questions/26481212/capture-ajax-response-with-selenium-and-python) – Swakeert Jain Jun 08 '16 at 19:42