如何从特定文件夹中的网站下载文件,我正在使用Chorme

时间:2017-10-10 05:38:05

标签: java selenium

我试图从必须保存在特定文件夹中的网站下载文件。网站http://bookboon.com/en/basics-of-accounting-information-processing-ebook 当我点击下载它将文件保存在下载部分时,我也尝试在chrome设置中更改下载目录,但它不起作用。我正在尝试自动化(selenium,java)。有什么办法吗?

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"));

    }
}

1 个答案:

答案 0 :(得分:0)

对于Chromedriver它会起作用

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);

对于Firefox:您需要setPreference

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

希望这会有所帮助。 :)