Cannot switch to new window using Selenium Webdriver using switchTo()

随声附和 提交于 2021-01-28 14:41:56

问题


I have searched and searched for an answer to this question. I am testing in IE8 using Selenium Webdriver and C#. 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.

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.

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


回答1:


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


来源:https://stackoverflow.com/questions/28809580/cannot-switch-to-new-window-using-selenium-webdriver-using-switchto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!