簡體   English   中英

如何使用Selenium WebDriver處理Excel下載彈出窗口

[英]How to handle Excel download popup using Selenium WebDriver

  • 如何使用Selenium WebDriver處理Excel下載彈出窗口

Excel下載彈出

您必須創建/修改瀏覽器配置文件才能自動下載/保存在彈出窗口中發生的excel文件(或可以使用Robot類處理窗口彈出窗口)。

請參閱如何使用Selenium Webdriver下載任何文件並將其保存到所需位置

我想你正在尋找這樣的東西

//common to all the cases
FirefoxProfile prof = new FirefoxProfile();

//Case:1 - Use this case to set download this code to your browser's default location
//prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");
//prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;");

//Case:2 - Download file to Desktop
//prof.setPreference("browser.download.folderList", 0);
//prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");
//prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;");


//Case:3 - Download to custom folder path. Replace d:\\selenium with your Download Location 
prof.setPreference("browser.download.dir","D:\\selenium\\");
prof.setPreference("browser.download.folderList", 2);
prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");


//Set Preference to not show file download confirmation dialogue using MIME types Of different file extension types.

prof.setPreference("browser.helperApps.neverAsk.saveToDisk", 
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;"//MIME types Of MS Excel File.
    + "application/pdf;" //MIME types Of PDF File.
    + "application/vnd.openxmlformats-officedocument.wordprocessingml.document;" //MIME types Of MS doc File.
    + "text/plain;" //MIME types Of text File.
    + "text/csv"); //MIME types Of CSV File.

//This will work for all cases mentioned above
WebDriver driver = new FirefoxDriver(prof);
driver.get("http://docs.seleniumhq.org/download/");
driver.findElement(By.xpath("//tr[1]/td[4]/a[text()='Download']")).click();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM