[英]Popup window handle in Selenium Web Driver using C#
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.
当我手动执行步骤并按Enter键时,它可以工作并且文件被保存,但是当我自动化时,什么也没发生。
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 问题是该窗口卡在父窗口中,并且我无法将“ Enter”发送到弹出窗口
Since, file download is not native to browser it is not possible to handle with Selenium
. 由于文件下载不是浏览器固有的,因此无法使用
Selenium
处理。 Remember, Selenium
is only a tool to automate browser. 记住,
Selenium
只是使浏览器自动化的工具。 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");
Problem Solved! 问题解决了! If anybody having issue to download file using selenium Webdriver, this is the solution.
如果有人在使用Selenium Webdriver下载文件时遇到问题,这就是解决方案。
[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脚本来完成
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.