简体   繁体   中英

Download files automatically in Internet Explorer 11 with Python and Selenium

I am trying to download some Excels files throught multiple Internet Explorer 11 windows at the same time by using Python and Selenium. The problem appens when the "Save As" Pop Up appears and the only way to click to save button is send keys (alt + s). But to do that, the focus must be over the browser window, and as I sais before, I need launch mulitples IE11 windows at the same time and doing the same thing.

Tools like AutoIt, Robot or just events send keys doesn't be valid because these tools use operating system, I think there must be a solution like javascript, or Python to handle each window browser that should works.

I appreciate yout Help, Thanks!!

As far as I know, WebDriver has no capability to access the IE Download dialog boxes presented by browsers when you click on a download link or button. But, we can bypass these dialog boxes using a separate program called " wget ".

By using this program, first, we could get the hyperlink href attribute value, and then, execute the Command Prompt Command to download the file from the link.

Sample code:

import time
import os
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

cap = DesiredCapabilities().INTERNETEXPLORER
cap['ignoreZoomSetting'] = True
driver = webdriver.Ie("D:\\Downloads\\webdriver\\IEDriverServer_x64_3.14.0\\IEDriverServer.exe",capabilities= cap)


driver.get("<website url>")

time.sleep(3)

btn = driver.find_element_by_id("btnDowloadReport")


hrefurl = btn.get_attribute("href")

os.system('cmd /c D:\\temp\\wget.exe -P D:\\temp --no-check-certificate ' + hrefurl)


print("*******************")

Html resource:

<a id="btnDowloadReport" href="https://github.com//sakinala/AutomationTesting/raw/master/samplefile.pdf" >Download</a>

[Note]: please remember to change to webdriver path and website url to your owns.

More detail information about using wget, you could refer to this article .

Tried in Java, file was dowloaded.

driver.findElement(By.xpath("//body")).sendKeys(Keys.chord(Keys.CONTROL, "j"));
Thread.sleep(2000);
Robot rb = new Robot();
rb.keyPress(KeyEvent.VK_ENTER);

Explaination:

  1. First click on the button / link to download the file.this pop up will be seen在此处输入图像描述
  2. Click "Ctrl+j" this will open the "View Downloads pop up". then click enter, since the focus will be on the recent file, it will be downloaded

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM