简体   繁体   中英

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#. 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

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

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.

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