I have a "File Export" link and when I click it, it is opening a new child window with options like "Open File","Save File" and OK or Cancel buttons. When I manually go through the steps and hit enter key it works and the file gets saved, but when I automate nothing happens.
This is my code:
//Click Export link
driver.FindElement(By.Id("ReportViewer1_ctl01_ctl05_ctl01")).Click();
//Switch to popup window
driver.SwitchTo().Window(Driver.Instance.WindowHandles.Last());
//Click Enter to accept save option
driver.SwitchTo().ActiveElement().SendKeys(Keys.Enter);
//Close child window
driver.Close();
//Switch back to Parent window
driver.SwitchTo().Window(Driver.Instance.WindowHandles.First());
The problem is that the window is stuck in the parent window and I am not able to send "Enter" to the popup window
Since, file download is not native to browser it is not possible to handle with Selenium
. Remember, Selenium
is only a tool to automate browser. However, you can handle this with setting up profile.
FirefoxProfile profile = new FirefoxProfile();
WebDriver driver = new FirefoxDriver(profile);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk" , "application/octet-stream;application/csv;text/csv;application/vnd.ms-excel;");
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.showWhenStarting",false);
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir","e:\\SampleExcel");
See this
Problem Solved! If anybody having issue to download file using selenium Webdriver, this is the solution.
[TestClass]
public class UnitTest1
{
[TestMethod]
public void FileDownload()
{
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");
IWebDriver driver = new FirefoxDriver(profile);
driver.Navigate().GoToUrl("http://www.joomla.org/download.html");
Thread.Sleep(3000);
driver.FindElement(By.Id("latest")).Click();
driver.Close();
}
}
这可以通过使用AutoIT脚本来完成
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.