I have got a webpage with several links to download files. Clicking on each link will open a new page with a bunch of links to download a number of files. I would like to create a separate folder for each of those links and download all the files under that link to the folder. I am using java, selenium to automate the process. I am ok with either of the following solutions:
Create a new directory based on link text. Change default download directory of Chrome to the new directory and download all the files under that link to newly created directory
Use java code to create a new directory based on link text and move all the files to new directory after download. ( sounds like more efficient solution )
I already have a code that can iterate through all the links and download files to the default download directory. But need help with creating individual download directories for each link. This is more than setting up a default download directory.
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
String fileDownloadPath = "C:\\Users\\username\\Documents\\Downloads";
//Set properties to supress popups
Map<String, Object> prefsMap = new HashMap<String, Object>();
prefsMap.put("profile.default_content_settings.popups", 0);
prefsMap.put("download.default_directory", fileDownloadPath);
prefsMap.put("plugins.always_open_pdf_externally", true);
//assign driver properties
ChromeOptions option = new ChromeOptions();
option.setExperimentalOption("prefs", prefsMap);
option.addArguments("--test-type");
option.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(option);
String strFirstWindowHandle = driver.getWindowHandle();
List<WebElement> listOfLinks =
driver.findElements(By.xpath("//a[contains(@href,'/portal/download1')]"));
for (WebElement link :listOfLinks )
{
// I would like to create a new directory based on each link text
link.click();
Thread.sleep(200);
List<WebElement> listOfDownloads = driver.findElements(By.xpath("//a[contains(@href,'.pdf')]"));
//Then download all the pdfs inside the newly created directory
for (WebElement link1 :listOfDownloads )
{
link1.click();
Thread.sleep(200);
}
}