Not able to download if download link attribute is _blank | Selenium Forum
M
Posted on 22/08/2016
Not able to download from a page using the following code if the download link has target attribute as '_blank' instead of '_self'. It opens a new window and then shows the 'Save As' dialog box instead of downloading the file in the given path mentioned in the code
[code:1admpe1g]FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);//0(desktop), 1(downloads folder) , or 2(specified dir)
profile.setPreference("browser.download.manager.showWhenStarting", false);// prevent Download Manager window
profile.setPreference("browser.download.dir", System.getProperty("user.dir")+"\\Data extracted");//path
//profile.setPreference("browser.helperApps.neverAsk.openFile",
//"text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet ");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);[/code:1admpe1g]

M
Replied on 23/08/2016

try this

package com.soapuitutorial.propertie;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

public class DownloadWithoutPrompt {
// http://stackoverflow.com/questions/34271396/how-to-handle-downloading-xlsx-file-in-firefox-using-webdriver-where-window-pop
public static void main(String[] args) {

FirefoxProfile profile = new FirefoxProfile();

profile.setPreference("browser.download.folderList", 2);

profile.setPreference("browser.download.manager.showWhenStarting",
false);

profile.setPreference("brower.download.dir", "C:\\temp"); // path of

profile.setPreference(
"browser.helperApps.neverAsk.saveToDisk",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");

profile.setPreference("browser.download.manager.showWhenStarting",
false);

profile.setPreference("browser.download.manager.alertOnEXEOpen", false);

WebDriver driver = new FirefoxDriver(profile);
driver.get("http://qtpselenium.com/test/testdownload.php");
driver.findElement(By.xpath("html/body/a[1]")).click();
driver.findElement(By.xpath("html/body/a[2]")).click();
driver.findElement(By.xpath("html/body/a[4]")).click();

}

}


M
Replied on 23/08/2016

This didn't work.


M
Replied on 23/08/2016

what kind of file are you trying to download?


M
Replied on 23/08/2016

Its an excel file. The behavior is like this once you click on the download link, it opens a new window and then displays the 'Save as' dialog box.


M
Replied on 23/08/2016

can i have the access to the link?


M
Replied on 23/08/2016

Sorry cannot share the url here, all I can share is that the download link has got the following HTML:

[code:3q5tzm52]<a target="_blank" href="/athena/bir/download/export/198">Download</a>[/code:3q5tzm52]

It works fine if the html code of the download link is:

[code:3q5tzm52]<a target="_self" href="/athena/bir/download/export/198">Download</a>[/code:3q5tzm52]


M
Replied on 24/08/2016

can you come on skype?


M
Replied on 24/08/2016

Sure, can we do that at 4 pm IST today?


M
Replied on 24/08/2016

ok


M
Replied on 25/08/2016

Ashish, please let me know once you get a solution to this. Thanks!


M
Replied on 26/08/2016

use this to get the mime type

add that to the list


[code:135w9pet]Path p1 = Paths
.get("path/to/.csv/file");
String x = Files.probeContentType(p1);
System.out.println(x);[/code:135w9pet]