0

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:

  1. 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

  2. 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);
            }

            }
Prem
  • 355
  • 2
  • 17
  • Possible duplicate of [How to set default download directory in selenium Chrome Capabilities?](https://stackoverflow.com/questions/34515328/how-to-set-default-download-directory-in-selenium-chrome-capabilities) – Ranielle Canlas Aug 13 '19 at 03:08
  • https://stackoverflow.com/questions/34515328/how-to-set-default-download-directory-in-selenium-chrome-capabilities – Ranielle Canlas Aug 13 '19 at 03:08
  • @RanielleCanlas it is not a duplicate to the links you have posted. If you read my code carefully, I am already doing what has been suggested in those links ( to setup a custom default download directory ). My problem is I need to be able to change it dynamically after it has been initially set. I am not sure if Selenium has got an elegant solution for it or I need to go back to native java io methods which will help me creating a new directory for each link and then moving the downloaded files to the new directory. – Prem Aug 13 '19 at 03:19
  • @RanielleCanlas will it be possible for you to change the duplicate tag on this question? this is not a duplicate question but has been tagged as duplicate. This is not about setting the download directory parameter only once. I would like to create a new download directory of each link. – Prem Aug 14 '19 at 04:04

0 Answers0