Retrieve xml file from the website and save it to a specific location using java or selenium with Chrome browser.
Here is the snippet of HTML code:
<!-- Start of Code which handles XML Download-->
<a href="javascript:downloadXML()">
<img src="/img/tabs/downloadxml.gif" alt="Download" name="imag34" width="40" height="20" border="0">
</a>
<!-- End of Code which handles XML Download-->
Simply click on the image to trigger the download process and have the file saved at your desired location.
Below is the customized configuration for Google Chrome:
File file = new File("resources/chromedriver.exe");
String downloadFilepath = "mydownload path";
Map<String, Object> preferences = new Hashtable<>();
String absolutePath = file.getAbsolutePath();
System.setProperty("webdriver.chrome.driver", absolutePath);
preferences.put("profile.default_content_settings.popups", 0);
preferences.put("download.prompt_for_download", "false");
preferences.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", preferences);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
Execution:
Upon clicking the image, the XML file downloads to the specified location but with a size of 0kb.
Question: Why is the file not fully downloading? Is there an error in my code?