简体   繁体   English

无法使用Selenium Webdriver通过switchTo()切换到新窗口

[英]Cannot switch to new window using Selenium Webdriver using switchTo()

I have searched and searched for an answer to this question. 我已经搜索了这个问题的答案。 I am testing in IE8 using Selenium Webdriver and C#. 我正在使用Selenium Webdriver和C#在IE8中进行测试。 At a certain point in the test I am required to click a button that will then open up a new application window with forms that need to be filled out to continue the test. 在测试中的某个时刻,我需要单击一个按钮,然后将打开一个新的应用程序窗口,其中包含需要填写的表格才能继续测试。

I have tried: 我努力了:

driver.switchTo().Window(driver.WindowHandles[0]);
driver.switchTo().Window(driver.WindowHandles[1]); //this one usually returns and error
driver.switchTo().Window(driver.WindowHandles.Last());

I have also tried 我也尝试过

foreach(String handle in driver.WindowHandles)
{
     driver.switchTo().Window(handle);
     //do things here
}

Each one fails and when I write the handle name to the console the outputs are all the same window name no matter what I try to do. 每个失败,当我将句柄名称写入控制台时,无论我做什么,输出都将是相同的窗口名称。 Does anyone have an idea as to how I can get the new window? 有人对我如何获得新窗口有想法吗?

Adding on to question: 补充问题:

Thank you @Saifur for the new direction with using PopupWindowFinder. 感谢@Saifur提供使用PopupWindowFinder的新方向。

This is what I have tried with it so far 到目前为止,这是我尝试过的

string currentWindow = driver.CurrentWindowHandle;
PopupWindowFinder finder = new PopupWindowFinder(driver);
string popWindow = finder.Click(driver.FindElementById("Element"));
driver.SwitchTo().Window(popWindow);

So this is supposed to click on the element that will begin the popup event and then the switch should place it on the popup window so I can continue the test. 因此,这应该单击将开始弹出事件的元素,然后开关应将其放置在弹出窗口中,以便我可以继续测试。 Here is the problem now. 这是现在的问题。 When the Element is clicked by finder the app I am testing on creates one popup, then that popup window is closed, then another popup appears then that is closed and finally the actual app I need to test on appears and is either the current focus on the monitor or behind the initial page that started all of this. 当通过finder单击Element时,正在测试的应用程序会创建一个弹出窗口,然后关闭该弹出窗口,然后出现另一个弹出窗口,然后将其关闭,最后出现需要测试的实际应用程序,并且该当前应用程序是监视器或启动所有这些操作的初始页面的后面。

So How can I get to the app window I need to test on? 那么,如何进入需要测试的应用程序窗口? I have tried to switch onto the different popup windows that appear to try and jump it to the app window, but that did not work. 我试图切换到显示为尝试将其跳至应用程序窗口的不同弹出窗口,但这没有用。 Thanks in advance 提前致谢

This might be helpful, It works charm: 这可能会有所帮助,它很有用:

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;

//To open new tab
js.ExecuteScript("window.open()");
ArrayList tabs = new ArrayList(driver.WindowHandles);

//Switch driver to new tab
driver.SwitchTo().Window(driver.WindowHandles[1]);

//Close new tab
driver.Close();

//Switch driver to old tab
driver.SwitchTo().Window(driver.WindowHandles[0]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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