Preferences set to avoid download dialog in firefox is not working for pdf - selenium

I have tried to set all possible preferences to avoid open and save file dialog while download files using selenium.
It is working for text files but not for PDF files
Below are the preferences set:
String downloadPath = <some random path>;
String mimetypes = "application/vnd.pdf,application/vnd.adobe.xfdf,text/csv,application/x-msexcel,application/excel,application/pdf,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml";
String url = "http://only-testing-blog.blogspot.in/2014/05/login.html";
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.panel.shown", false);
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.download.dir", downloadPath);
profile.setPreference("browser.helperApps.neverAsk.openFile", mimetypes);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", mimetypes);
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.download.manager.focusWhenStarting", false);
profile.setPreference("browser.download.manager.useWindow", false);
profile.setPreference("browser.download.manager.showAlertOnComplete", false);
profile.setPreference("browser.download.manager.closeWhenDone", false);
FirefoxDriver driver = new FirefoxDriver(profile);
driver.get(url);
driver.findElement(By.xpath("//a[contains(.,'Download PDF File')]")).click();
Am using Firefox 46 with 2.53.0 selenium version
Please help me to make this to work for PDF, Excel and Word files as well
Thanks!

One solution I found for this is to use the ProfileIni class. It allows you to load a premade firefox profile. You use as shown below
private ProfilesIni init = new ProfilesIni();
private FirefoxProfile profile = init.getProfile("QA");
private WebDriver driver = new WebDriver(profile);
Then, you can set all of those preferences straight on the profile, and set the setting to skip the download dialogue and automatically save to the destination folder you want and keep the settings for every test case you need to download stuff on

Related

Download Excel file from Firefox using Selenium [duplicate]

I am trying to download an Excel file from Firefox and Webdriver, but i can't handle the download pop-up.
When click on button i need the file to download automatically, without showing pop-up.
Here is my code:
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.download.folderList", 2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
firefoxProfile.setPreference("browser.download.dir", Constant.Downloaded_Path);
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv/xls/xlsx");
firefoxProfile.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");
firefoxProfile.setPreference("browser.helperApps.alwaysAsk.force", false);
firefoxProfile.setPreference("browser.download.manager.alertOnEXEOpen", false);
firefoxProfile.setPreference("browser.download.manager.focusWhenStarting", false);
firefoxProfile.setPreference("browser.download.manager.useWindow", false);
firefoxProfile.setPreference("browser.download.manager.showAlertOnComplete", false);
firefoxProfile.setPreference("browser.download.manager.closeWhenDone", false);
return firefoxProfile;
But, the above code is not working. Can any one help?
First you need to get the mime type corresponding to the file:
Open Developer Tools and then the Network tab
Go back to the page and click on the file to download
Go back to the network panel and select the first request
Copy the mime type on the right of Content-Type from the response header:
Set the preference "browser.helperApps.neverAsk.saveToDisk" with your mime type
Make sure the download folder "browser.download.dir" exists
Here is a working example with Firefox:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "C:\\Windows\\temp");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.ms-excel");
profile.setPreference("pdfjs.disabled", true); // disable the built-in PDF viewer
WebDriver driver = new FirefoxDriver(profile);
driver.get("http://www.exinfm.com/free_spreadsheets.html");
driver.findElement(By.linkText("Capital Budgeting Analysis")).click();
FirefoxProfile profile = new FirefoxProfile();
// profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", " text/plain, application/octet-stream doc xls pdf txt");
profile.SetPreference("browser.download.manager.alertOnEXEOpen", false);
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/csv, text/csv, text/plain,application/octet-stream doc xls pdf txt");
profile.SetPreference("browser.download.manager.focusWhenStarting", false);
profile.SetPreference("browser.download.useDownloadDir", true);
profile.SetPreference("browser.helperApps.alwaysAsk.force", false);
profile.SetPreference("browser.download.manager.closeWhenDone", true);
profile.SetPreference("browser.download.manager.showAlertOnComplete", false);
profile.SetPreference("browser.download.manager.useWindow", false);
profile.SetPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
profile.SetPreference("pdfjs.disabled", true);
_driverInstance = new FirefoxDriver(profile);
These settings worked for me. Hope it might help you.
you need to provide mimetypes for the application you want to download.
For python, if you want to enable it for all applications then use the following code
import mimetypes
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", ','.join(list(it for it in mimetypes.types_map.values())))

Is there any egde webdriver documentation about preferences?

I am currently trying to create cross-browser automated tests with Geb but I cannot find any documentation about preferences with Edge. What I am trying to do is to set up my Edge environment to automatically download documents and save them in downloads/edge.
I have done this way for chrome and firefox:
customChrome {
driver = {
System.setProperty("webdriver.chrome.driver", new File ("Drivers/chromedriver_win32/chromedriver.exe").getAbsolutePath())
Map<String, Object> chromePrefs = new HashMap<String, Object>()
chromePrefs.put("download.default_directory", new File("downloads/chrome").getAbsolutePath())
chromePrefs.put("download.prompt_for_download", false)
chromePrefs.put("plugins.always_open_pdf_externally", true)
ChromeOptions opt = new ChromeOptions()
opt.setExperimentalOption("prefs", chromePrefs)
new ChromeDriver(opt)
}
}
customFF {
driver = {
FirefoxProfile myProfile = new FirefoxProfile()
myProfile.setPreference("browser.helperApps.alwaysAsk.force", false)
myProfile.setPreference("browser.download.manager.showWhenStarting", false)
myProfile.setPreference("browser.download.folderList", 2)
myProfile.setPreference("browser.download.dir", new File("downloads/firefox").getAbsolutePath()) // my downloading dir
myProfile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false)
myProfile.setPreference("browser.download.useDownloadDir", true)
myProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf, image/jpeg")
myProfile.setPreference("pdfjs.disabled", true)
System.setProperty("webdriver.gecko.driver", new File("Drivers/GeckoDriver/geckodriver.exe").getAbsolutePath())
new FirefoxDriver(myProfile)
}
}
I used this source file to get chrome's prefs and this webpage for getting those of firefox but I cannot find anything similar for Edge. Does Microsoft provides any info about this ?
Please share any info you may have.
I'm not sure if things have changed since, but according to this answer
IE doesn't use profiles and is therefore unable to download files to a specific location.

How to automatically close firebug tab when launching selenium firefox webdriver with firebug?

I have included firebug when launching firefox driver and it works very fine with latest selenium web driver 3.0 but meanwhile it also opens new firebug tab every time when launching browser.
As code says, i have included firebug file and added this extension in created profile. Is there any way to close the firebug tab automatically after launching the browser? If there is no automatic way then i need to use tweak to close window named "Firebug" right?
Code:
File file = new File("./firebug-2.0.17-fx.xpi");
System.setProperty("webdriver.gecko.driver", config.getStringProperty("geckodriver.path"));
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(file);
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
capabilities.setCapability("marionette", true);
webDriver = new FirefoxDriver(capabilities);
You can set the "showFirstRunPage" flag to "false" in your porfile.
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("extensions.firebug.showFirstRunPage", false);
Here is a link to all firebug preferences you can set.
Firebug Preferences
An other solution is to set the "currentVersion" to a big number like this.
profile.setPreference("extensions.firebug.currentVersion", "999");
I prefer the first on ;)
I was looking for automatically closing Firebug window but i think its not possible so posting an answer to handle opened windows after launching firebox browser with firebug capabilities, unfortunately you need to deal with extra lines of code due to this issue :)
Below solution it works fine, just use it like below:
Working code:
File file = new File("./firebug-2.0.17-fx.xpi");
System.setProperty("webdriver.gecko.driver", config.getStringProperty("geckodriver.path"));
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(file);
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
capabilities.setCapability("marionette", true);
webDriver = new FirefoxDriver(capabilities);
// Close the firebug window
Thread.sleep(4000); // Firebug window takes time to open it
Set <String> windows = webDriver.getWindowHandles();
String mainwindow = webDriver.getWindowHandle();
for (String handle: windows) {
webDriver.switchTo().window(handle);
if (!handle.equals(mainwindow)) {
webDriver.close();
}
}
webDriver.switchTo().window(mainwindow);

Selenium and Firefox profile setting

I need your help to set up my Firefox profile with Firebug. What I'd like to have is to have the Firebug add-on to be loaded with the Firefox instance when I start it via Selenium WebDriver. Here is sample of my code:
final File file = new File("C:\\Program Files (x86)\\Gecko\\bin\\geckodriver.exe");
System.setProperty("webdriver.gecko.driver", file.getAbsolutePath());
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("?");
So... how can I set the setPreference value, so that Firebug is loaded together with Firefox when the Selenium WebDriver starts the browser?
You have to add the extension instead of setting the preference:
final String firebugPath = "C:\\FF_Profile\\firebug.xpi";
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(new File(firebugPath));
WebDriver driver = new FirefoxDriver(profile);
You can find the Firebug path following this instructions:
https://stackoverflow.com/a/8074828/432681
Kotoj
Here is the code:
// Tell cucumber where the geckodriver is located in your environment
final File file = new File("C:\\Program Files (x86)\\Gecko\\bin\\geckodriver.exe");
// Get the absolute path to the Gecko Driver
System.setProperty("webdriver.gecko.driver", file.getAbsolutePath());
final String firebugPath= "C:\\Users\\<My username>\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\erx927l4.default\\extension s\\firebug#software.joehewitt.com.xpi";
// Set up the Firefox browser profile
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(new File(firebugPath));
// bring up the browser
driver = new FirefoxDriver(profile);
I tried to add these codes to the comments section but it won't let me format the text so I have to post them in the "Post Your Answer" section.. sorry

Handle download confirmation popup/dialog in Firefox

I have a test to upload ,compress and download pdf files. Everything is working fine except the download part. I am not able to get how to handle the confirmation popup/Save or Open file dialog in Firefox when we click on download. I tried modifying firefox config but still not able to solve it. Can anyone please help?
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.download.dir", "C:\\Users\\Sahil\\Downloads\\");
profile.setPreference("browser.helperApps.neverAsk.openFile","application/pdf");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/pdf");
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.download.manager.focusWhenStarting", false);
profile.setPreference("browser.download.manager.useWindow", false);
profile.setPreference("browser.download.manager.showAlertOnComplete", false);
profile.setPreference("browser.download.manager.closeWhenDone", false);
profile.setPreference("browser.download.manager.useWindow", false);
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
, 0 profile.setPreference("pdfjs.disabled", true);
WebDriver driver=new FirefoxDriver(profile);
driver.get("http://www.ilovepdf.com/compress_pdf");
driver.findElement(By.id("pickfiles")).click();
Runtime.getRuntime().exec("C:\\Users\\Sahil\\Documents\\Au\\Second.exe");
WebDriverWait wait=new WebDriverWait(driver, 100);
WebElement element1=wait.until(ExpectedConditions.elementToBeClickable(By.id("uploadfiles")));
element1.click();
WebElement element=wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("a#download")));
element.click();
You have a lot of preferences that you do not need. All you need is this:
// Create a firefoxprofile for firefox specific settings
FirefoxProfile profile = new FirefoxProfile();
// Set the downloads folder
profile.setPreference("browser.download.dir", path/to/folder);
// Download files to the downloads folder
profile.setPreference("browser.download.folderList", 2);
// Don't show downloads window when download starts
profile.setPreference("browser.download.manager.showWhenStarting", False);
// Prevent file download dialog to be shown for certain MIME-types
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
If this doesn't work you should make sure you have correct MIME-type specified. You can do this monitoring the network in your browser and download the file manually. A GET request should be made with a specific content-type. Make sure that content-type has been added to your browser.helperApps.neverAsk.saveToDisk preference.
Edit!
I just checked the MIME-type for the website you have specified. You are trying auto-saving a application/pdf. However you are downloading a file with MIME-type application/octet-stream.
You need to change this:
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
To this:
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");
How to find the MIME-type
Before you hit the download button open your firefox console and go to the network tab.
Now if you hit the download button you will see a GET request has been made. If you open this request you can see the content-type of the response. This is your MIME-type.