Selenium webdriver 窗口处理 c# switchTo 失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11237357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium webdriver window handles c# switchTo failed
提问by user1487331
Here comes 2 windows pop out during the testing.
在测试过程中弹出 2 个窗口。
my code:
我的代码:
string BaseWindow = driver.CurrentWindowHandle;
ReadOnlyCollection<string> handles = driver.WindowHandles;
foreach(string handle in handles)
{
Boolean a = driver.SwitchTo().Window(handle).Url.Contains("Main");
if (a == true)
{
InitialSetting.driver.SwitchTo().Window(handle);
break;
}
}
I want to switch to the window which url contains "Main". But when the test is running, it switches between two windows continuously and it doesn't stop.
我想切换到 url 包含“Main”的窗口。但是当测试运行时,它会在两个窗口之间不断切换并且不会停止。
I debug and found the foreachdidn't break even when the boolean ais true.
我调试并发现foreach当boolean a为真时并没有收支平衡。
How can I resolve this?
我该如何解决这个问题?
回答by Michael Ayers
//switch to new window
driver.FindElement(By.Id("link")).Click();
//wait for new window to open
Thread.Sleep(2000);
//get the current window handles
string popupHandle = string.Empty;
ReadOnlyCollection<string> windowHandles = driver.WindowHandles;
foreach (string handle in windowHandles)
{
if (handle != existingWindowHandle)
{
popupHandle = handle; break;
}
}
//switch to new window
driver.SwitchTo().Window(popupHandle);
//check for element on new page
webElement = driver.FindElement(By.Id("four04msg"));
if(webElement.Text == "THE CONTENT YOU REQUESTED COULDN'T BE FOUND...")
{
return false;
}
else
{
return true;
}
//close the new window to navigate to the previous one
driver.close();
//switch back to original window
driver.SwitchTo().Window(existingWindowHandle);
回答by dmunozpa
Using the original post code.
string existingWindowHandle = driver.CurrentWindowHandle;Its the first window.
One important thing is:
ReadOnlyCollection<string> windowHandles = driver.WindowHandlesContains the string name object, not the Windows Title Name, for example Collection
windowHandlescould contains:Not Windows Title Name as
{Menu},{PopUp}
It contains:{45e615b3-266f-4ae0-a508-e901f42a36d3},{c6010037-0be6-4842-8d38-7f37c2621e81}
使用原始邮政编码。
string existingWindowHandle = driver.CurrentWindowHandle;它的第一个窗口。
一件重要的事情是:
ReadOnlyCollection<string> windowHandles = driver.WindowHandles包含字符串名称对象,而不是 Windows 标题名称,例如 Collection
windowHandles可以包含:不是 Windows 标题名称,因为
{Menu},{PopUp}
它包含:{45e615b3-266f-4ae0-a508-e901f42a36d3},{c6010037-0be6-4842-8d38-7f37c2621e81}
回答by William
IWebDriver popup = null;
string mainWindow = driver.CurrentWindowHandle;
bool foundPopupTitle = false;
foreach (string handle in driver.WindowHandles)
{
popup = driver.SwitchTo().Window(handle);
if (popup.Title.Contains(title))
{
foundPopupTitle = true;
break;
}
}
if (foundPopupTitle)
{
popup.Close();
}
//switch back to original window
driver.SwitchTo().Window(mainWindow);
回答by PABLO ANDRES HOYOS ZULUAGA
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("window.open()");
String ventanaPrincipal = driver.CurrentWindowHandle;
List<string> listWindow = new List<string>(driver.WindowHandles);
driver.SwitchTo().Window(listWindow[1]);
driver.Navigate().GoToUrl("http:www.google.com");
IWebElement search = driver.FindElement(By.Name("q"));
search.SendKeys("RPA");
回答by Deiveegarajan G
string NewWindowHandle = string.Empty;
ReadOnlyCollection<string> windowHandles = driver.WindowHandles;
NewWindowHandle = windowHandles[windowHandles.Count - 1];
driver.SwitchTo().Window(NewWindowHandle);

