I'm trying to download CSV file with Selenium on Firefox.
I saw several similar posts about this issue here, so my settings are already as following:
String downloadsPath = getFromRepository("common","//downloadsPath");
System.setProperty("webdriver.gecko.driver", "./src/main/resources/geckodriver.exe");
FirefoxProfile profile = new FirefoxProfile();
FirefoxOptions options = new FirefoxOptions();
profile.setPreference("browser.download.dir",downloadsPath);
profile.setPreference("browser.download.folderList",2);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/csv,application/excel,application/vnd.ms-excel,application/vnd.msexcel,text/anytext,text/comma-separated-values,text/csv,text/plain,text/x-csv,application/x-csv,text/x-comma-separated-values,text/tab-separated-values");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/xml,text/plain,text/xml,image/jpeg,application/octet-stream");
profile.setPreference("browser.download.manager.showWhenStarting",false);
profile.setPreference("browser.helperApps.neverAsk.openFile","application/csv,application/excel,application/vnd.ms-excel,application/vnd.msexcel,text/anytext,text/comma-separated-values,text/csv,text/plain,text/x-csv,application/x-csv,text/x-comma-separated-values,text/tab-separated-values");
profile.setPreference("browser.helperApps.neverAsk.openFile","application/xml,text/plain,text/xml,image/jpeg,application/octet-stream");
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
options.setProfile(profile);
driver = new FirefoxDriver(options);
But all this doesn't work, I still see the pop-up dialog
I guess the problem here is because this CSV file is not actually downloaded from some server but is generated on the client side by file-saver JS
I also see in dev tools no traffic when this CSV file is downloaded.
I will appreciate any help.
After some researches I found that for this case data:text/csv mimeType should be added to the profile preferences.
Now it works smooth!
Related
I use WebDriverManager.chromedriver().setup(); for getting chrome property in my selenium tests. It works fine. I am trying to download a file by changing the default download location of chrome browser because I want to download the file to my java project classpath, rather than to my local machine, but I am unsure if WebDriverManager has such an implementation. Currently, I am trying something like this:
WebDriverManager.chromedriver().setup();
String downloadDir = System.getProperty("user.dir");
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("download.default_directory", downloadDir);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(cap);
The code block works and downloads the file to the project classpath, as expected, but I think there would be a cleaner and shorter way to do it. I have done some research on the Bonigarcia WebDriverManagerdependency and some of its implementations but can't find anything helpful. Is there a better way to achieve the above?
Use the method targetPath() to change the default location of the driver downloaded by WebDriverManager:
WebDriverManager.chromedriver().targetPath("/my/custom/path").setup();
I am trying to automate file download functionality using Selenium WebDriver. I am using Google Chrome and the type of the file to download is of PDF format. When WebDriver clicks on the download (or Print) link, browser shows the preview of the pdf file instead of downloading it directly. How can I make the chrome driver to download pdf files directly?.
I tried the below code, but no luck
ChromeOptions options = new ChromeOptions();
Map<String,Object> preferences = new HashMap<>();
preferences.put("pdfjs.disabled", true);
options.setExperimentalOption("prefs", preferences);
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
WebDriver driver=new ChromeDriver(options);
I know this question has already asked on StackOverflow, including this, but none of these solutions work for me.
I am using - Google Chrome v54.0.2840.99, Chromedriver v2.25 and Selenium v3.0.1
HTML of the Download/Print link is shown below
This problem can be solved by adding the following attributes to the download/print element
download=""
target="_blank"
This can be done using javascript as follows
WebElement printLink=driver.findElements(By.linkText("Print")).get(0);
JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute(arguments[1],arguments[2])",printLink,"download","");
js.executeScript("arguments[0].setAttribute(arguments[1],arguments[2])",printLink,"target","_blank");
You can set the attribute download of the a element, and then click on the element. See code below:
String script = "document.querySelector('td a[href*=\"/print/\"]').setAttribute('download','name-of-the-download-file-recommend-guid-or-timestamp.pdf');";
((JavascriptExecutor)driver).executeScript(script);
driver.findElement(By.cssSelector("td a[href*='/print/']")).click();
I have a critical issue over here.
Please find my scenario below:
login
click on a link
after the click, a new tab opens
I have switched the focus to the new opened tab with the following code
ArrayList<String> newTab = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(newTab.get(1));
The issue is that when I try to click on an excel download link on the newly opened tab, the "Open with" popup is appearing and my automation fails. Even after adding the following preference
firefoxProfile.setPreference("browser.helperApps.neverAsk.openFile",
"text/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"text/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
Please, can you suggest a feasible solution as soon as possible?
Thank you
Please manually set these preferences in your firefox's(about:config) section, visit the application, click the link and see if the file gets downloaded without any prompt. This will help you to identify the issues with automation.
I tried setting these preferences in my firefox, but it still prompts download window. I can download without prompt only after checking "Do this automatically for files like this from now on" which updates mimeTypes.rdf file in the profile directory. So to make this work through automation, you may need to bundle a custom firefox profile that includes a mimeTypes.rdf with your TestSuite.
Here's the code to create FirefoxProfile from a given profile directory:
FirefoxProfile profile = new FirefoxProfile(new File("<PATH_TO_FIREFOX_PROFILE_DIRECTORY_THAT_WORKS_WHEN_TESTED_MANUALLY>");
WebDriver driver = new FirefoxDriver(profile);
I recently started up coding with Selenium and Java. I have a basic test set up and things seem to be working with Firefox. I would like test on Chrome as well. But when I define the Webdriver as ChromeDriver, I get an error saying I need to define it on the system path.
I used Maven to download all the dependencies, but now I don't know how to reference them properly.
My issue:
protected void setUpBeforeTestClass(){
// define path to ChromeDriver
// cause I get the error "The path to the driver executable must be set by the webdriver.chrome.driver system property"
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
// driver = new FirefoxDriver();
driver = new ChromeDriver();
String url = urls[0]; // pull in from array of urls
driver.get(url);
}
Maven downloads dependencies to:
C:\Users\{username}\.m2\repository\org\seleniumhq\selenium\ ...
And ChromeDriver is in that folder.
How can I reference this folder to pull in ChromeDriver without hard-coding the path? (I'm not looking to modify my system environment variables)
My goal is that I can just download my Java classes and Maven dependencies on any machine and run the tests.
You need to download the Chrome Driver Binary and put it somewhere on your computer. Somewhere like "C:/Selenium/chromedriver.exe". You can find it here. You can then access it by using something like:
System.setProperty("webdriver.chrome.driver", "C:/Selenium/chromedriver.exe"));
As per answers, I found that it is the binary that I was missing. Damn.
I found this:
https://github.com/bonigarcia/webdrivermanager
This helps out a lot in terms of managing the webdrivers I want to use. I don't have to download the webdrivers myself, this does it for me.
Download the binary from here:-
http://chromedriver.storage.googleapis.com/index.html?path=2.19/
Use below code:-
WebDriver driver=null;
System.setProperty("webdriver.chrome.driver","./src//lib//chromedriver");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
capabilities.setCapability("chrome.binary","./src//lib//chromedriver");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
Hope it help :)
Get back to me if still facing issue :)
How do I configure Selenium WebDriver? I have automated test cases using Selenium with Java. Now I need to automate upload and download of a file using WebDriver. I had added webdriver-common-0.9.7376.jar. I like to use Internet Explorer. How can I do that?
I'm just declaring variable and using driver
private static WebDriver driver;
driver.findElement(By.id(upload)).sendKeys("file to be upload");
Is this correct?
Ques. 1: How to configure WebDriver?
Ans: There are 2 ways: 1) Adding "selenium-server-standalone-2.29.0.jar" only
OR,
2) Adding "selenium-java-2.29.0.jar" and all the jars located on "selenium-java-2.29.0\selenium-2.29.0\libs" folder
You can download "selenium-server-2.29.0.zip" and "selenium-java-2.29.0.zip" from http://code.google.com/p/selenium/downloads/detail?name=selenium-server-2.29.0.zip and http://code.google.com/p/selenium/downloads/detail?name=selenium-java-2.29.0.zip respectively.
Extract them and you could get corresponding jar files to add.
Ques. 2: How to instantiate IE and how to upload file?
Ans: The java code as below:
File file = new File("C:\\Program Files\\Internet Explorer\\iexplore.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver = new InternetExplorerDriver();
driver.findElement(By.id("upload")).sendKeys("file to be upload");
If "File file = new File("C:\Program Files\Internet Explorer\iexplore.exe");" doesn't work download "IEDriverServer" and replace that line with below:
File file = new File("E:\\Ripon\\IEDriverServer_Win32_2.29.1\\IEDriverServer.exe");
[Note: You can download "IEDriverServer" from http://code.google.com/p/selenium/downloads/list ]
You need to add all jar after downloading selenium-java 2.25 0r any version. First add all jar then all all lib folder jar.
selenium-java-2.25.0.jar
selenium-java-2.25.0-srcs.jar and then all lib jar (Don't forget to add all lib folder jar)
Without instantiate driver for your browser, it won't open a browser window to do the upload/download operation. If you're using IE you've to write driver = new InternetExplorerDriver();
Instead of the old and outdated webdriver-common package, you probably need the newest selenium-java from http://code.google.com/p/selenium/downloads/list.
If you'll ever also need running Selenium RC locally, or Remote WebDriver ot Selenium Grid, you'll need the selenium-server package there (if you don't yet know what these are, just take selenium-java).
In both cases, for running InternetExplorerDriver, you'll also need the IEDriverServer from the page mentioned above. It's up to you whether to use the 32 or 64 bit version.
You can find an example of setting it up here in the documentation. If you dig around a bit, you'll find many more useful information in that documentation.
For example, for Internet explorer, you'll do:
System.setProperty("webdriver.ie.driver", "C:\\path\\to\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
// your testing code
driver.quit();
Your method of uploading a file is correct.
And as of now (Selenium v2.29.0), you can't download files via any WebDriver. If you really want to do so, you'll have to find another way.