硒的webdriver窗口句柄C#SWITCHTO失败 [英] selenium webdriver window handles c# switchTo failed

查看:382
本文介绍了硒的webdriver窗口句柄C#SWITCHTO失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面来2个窗口在测试过程中蹦出



我的代码:

 字符串BaseWindow = driver.CurrentWindowHandle; 
ReadOnlyCollection还<串GT;处理= driver.WindowHandles;

的foreach(在把手字符串句柄)
{
一个Boolean = driver.SwitchTo()窗口(句柄).Url.Contains(主)。
如果(A == true)而
{
InitialSetting.driver.SwitchTo()窗口(句柄)。
中断;
}
}



我想切换到哪个URL包含窗口主要。但是,当测试运行时,两个窗口之间切换不断,并没有停下来。



我调试,发现的foreach 没破,即使在 Boolean一个是真实的。



我怎样才能解决这个问题?


解决方案

  //切换到新窗口
driver.FindElement(通过。 。ID(链接))点击();

//等待新窗口中打开
Thread.sleep代码(2000);

//获取当前窗口句柄
串popupHandle =的String.Empty;
ReadOnlyCollection还<串GT; windowHandles = driver.WindowHandles;

的foreach(在windowHandles字符串句柄)
{
如果(手柄= existingWindowHandle!)
{
popupHandle =手柄;打破;
}
}

//切换到新窗口
driver.SwitchTo()窗口(popupHandle)。

//检查在新的页面
元素webElement = driver.FindElement(By.Id(four04msg));
如果(webElement.Text ==您所请求的内容找不到...)
{
返回FALSE;
}
,否则
{
返回真;
}

//关闭新窗口浏览前一个
driver.close();

//切换回原来的窗口
driver.SwitchTo()窗口(existingWindowHandle)。


Here comes 2 windows pop out during the testing.

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.

I debug and found the foreach didn't break even when the boolean a is true.

How can I resolve this?

解决方案

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

这篇关于硒的webdriver窗口句柄C#SWITCHTO失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆