简体   繁体   中英

How to download a file from a website in a specific folder, I am using Chorme

I'm trying to download a file from a website that has to be saved in a specific folder. Website http://bookboon.com/en/basics-of-accounting-information-processing-ebook When I click on download it saves the file in the download section, I also tried to change the download directory in chrome settings, it doesn't work. I am trying automation (selenium, java). Is there any way?

public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\User_2\\Downloads\\chromedriver_win32\\chromedriver.exe");
        d = new ChromeDriver();
        d.get("http://bookboon.com/en/basics-of-accounting-information-processing-ebook");

            d.findElement(By.id("email")).sendKeys("asd@ymail.com");
            WebElement One=d.findElement(By.xpath("html/body/div[1]/div/article/div/section[1]/form/div[2]/div[2]/div[1]/input"));
            One.sendKeys("Studying");
            One.sendKeys(Keys.TAB);

            WebElement Two=d.findElement(By.xpath("html/body/div[1]/div/article/div/section[1]/form/div[2]/div[2]/div[2]/input"));
            Two.sendKeys("Engineer/Science MSc");
            Two.sendKeys(Keys.TAB);

            WebElement Three=d.findElement(By.xpath("html/body/div[1]/div/article/div/section[1]/form/div[2]/div[2]/div[3]/input"));
            Three.sendKeys("All India Institute of Medical Sciences (AIIMS), Delhi");
            Three.sendKeys(Keys.TAB); 





            d.navigate().back();
            downlinks = d.findElements(By.className("pdf"));

    }
}

For Chromedriver It will work

String downloadFilepath = "/path/to/download";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(cap);

For Firefox: You need to setPreference

    profile.setPreference("browser.download.dir", "Filepath");

Hope this helps. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM