eclipse 在 Selenium Webdriver 中从子窗口切换到父窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26935456/
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
Switch from child to parent window in Selenium Webdriver
提问by Andy Tilston
I'm creating test scripts in Selenium WebSriver using Eclipse, and have hit a snag in a scenario whereby I have a parent window, I click on a link on that window and a child window opens. I then want to close the child window and carry out functions again on the parent window.
我正在使用 Eclipse 在 Selenium WebSriver 中创建测试脚本,并且在我有一个父窗口的场景中遇到了障碍,我单击该窗口上的链接并打开一个子窗口。然后我想关闭子窗口并在父窗口上再次执行功能。
My code is as below:
我的代码如下:
public static void daysInStockSelectContract(InternetExplorerDriver driver) {
driver.findElement(By.xpath(".//*[@id='page-content']/table/tbody/tr[1]/td[1]/a")).click();
for(String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
driver.close();
}
}
When I run the above code, the child window remains open whilst the parent window closes, which is the opposite effect of what I wanted. Also an error as follows is shown in the console:
当我运行上面的代码时,子窗口保持打开而父窗口关闭,这与我想要的效果相反。控制台中还显示如下错误:
"Exception in thread "main" org.openqa.selenium.remote.SessionNotFoundException: session 1ff55fb6-71c9-4466-9993-32d7d9cae760 does not exist"
“线程“主”org.openqa.selenium.remote.SessionNotFoundException 中的异常:会话 1ff55fb6-71c9-4466-9993-32d7d9cae760 不存在”
I'm using IE WebDriver for my Selenium scripts.
我正在为我的 Selenium 脚本使用 IE WebDriver。
UPDATE - 17/11/14
更新 - 17 年 11 月 14 日
Subh, here is the code I used from the link you kindly send over which unfortunately doesn't appear to work.
Subh,这是我从您发送的链接中使用的代码,不幸的是它似乎不起作用。
public static void daysInStockSelectContract(InternetExplorerDriver driver) {
//get the parent handle before clicking on the link
String winHandleBefore = driver.getWindowHandle();
driver.findElement(By.xpath(".//*[@id='page-content']/table/tbody/tr[1]/td[1]/a")).click();
// the set will contain only the child window now. Switch to child window and close it.
for(String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
driver.close();
driver.switchTo().window(winHandleBefore);
}
回答by Subh
Probably, the switching to child window didn't happen properly.
可能,切换到子窗口没有正确发生。
Hence, 'driver.close()'is closing the parent window, instead of child window. Also, since the parent window has been closed, it implies the session has been lost which gives the error 'SessionNotFoundException'.
因此,'driver.close()'正在关闭父窗口,而不是子窗口。此外,由于父窗口已关闭,这意味着会话已丢失,从而产生错误'SessionNotFoundException'。
This link will help you out in properly switching between windows
此链接将帮助您在窗口之间正确切换
On another note, just an advice. Rather than passing "driver"as a parameter, why don't you make it a static variable. It will be easily accessible to all the methods inside the class and it's subclasses too, and you don't have to bother about passing it each time to a method.. :)
另一方面,只是一个建议。与其将“驱动程序”作为参数传递,不如将其设为静态变量。类中的所有方法及其子类都可以轻松访问它,而且您不必每次都将它传递给方法。:)
Below is the code that you've requested in your comment (Unrelated to the question above)
以下是您在评论中要求的代码(与上述问题无关)
public class Testing_anew {
public static WebDriver driver;
public static void main(String args[]) throws InterruptedException{
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
public static void testmethod(){
driver.findElement(By.xpath("//some xpath")).click();
}
Updated Code 19/11/14
更新代码 19/11/14
public static void daysInStockSelectContract(InternetExplorerDriver driver) {
//get the parent handle before clicking on the link
String winHandleBefore = driver.getWindowHandle();
System.out.println("Current handle is: "+winHandleBefore);
driver.findElement(By.xpath(".//*[@id='page-content']/table/tbody/tr[1]/td[1]/a")).click();
// Iterating through the set of window handles till the child window's handle, that is infact
// not equal to the current window handle, is found
for(String winHandle : driver.getWindowHandles()) {
if(!winHandle.equals(winHandleBefore)){
System.out.println("Child handle is: "+ winHandle);
//Sleeping for 4 seconds for detecting Child window
try{
Thread.sleep(4000);
}catch(InterruptedException e){
System.out.println("Caught exception related to sleep:"+e.getMessage());
}
driver.switchTo().window(winHandle);
break;
}
}
System.out.println("AFTER SWITCHING, Handle is: "+driver.getWindowHandle());
driver.close();
//Sleeping for 4 seconds for detecting Parent window
try{
Thread.sleep(4000);
}catch(InterruptedException e){
System.out.println("Caught exception related to sleep:"+e.getMessage());
}
driver.switchTo().window(winHandleBefore); // Switching back to parent window
System.out.println("NOW THE CURRENT Handle is: "+driver.getWindowHandle());
//Now perform some user action here with respect to parent window to assert that the window has switched properly
}
回答by Sighil
When you loop through the windowHandles, the first handle is the parent handle. So when you say driver.close()
, the parent window is getting closed. This can be avoided by using the following code:
当您遍历 windowHandles 时,第一个句柄是父句柄。因此,当您说 时driver.close()
,父窗口正在关闭。这可以通过使用以下代码来避免:
public static void daysInStockSelectContract(InternetExplorerDriver driver) {
//get the parent handle before clicking on the link
String parentHandle = driver.getWindowHandle();
driver.findElement(By.xpath(".//*[@id='page-content']/table/tbody/tr[1]/td[1]/a")).click();
//get all the window handles and assign it to a set
//It will include child window and parentwindow
Set<String> windowHandles = driver.getWindowHandles();
System.out.println("No of Window Handles: " + windowHandles.size());
//remove the parent handle from the set
windowHandles.remove(parentHandle);
// the set will contain only the child window now. Switch to child window and close it.
for(String winHandle : driver.getWindowHandles()) {
strong textdriver.switchTo().window(winHandle); driver.close(); } }
强文本driver.switchTo().window(winHandle); 驱动程序关闭();} }
If you get the size as 1 (no of windowHandles as one) or is getting same error message, then probably the window is an alert box. To handle the alert box you can use the following code just after clicking on the link.
如果您得到的大小为 1(windowHandles 没有为 1)或收到相同的错误消息,则该窗口可能是一个警告框。要处理警报框,您可以在单击链接后使用以下代码。
Alert alert = driver.switchTo().alert();
alert.accept();
Try this code for debugging
试试这个代码进行调试
public static void daysInStockSelectContract(InternetExplorerDriver driver) {
// get the parent handle before clicking on the link
String parentHandle = driver.getWindowHandle();
System.out.println("ParentHandle : " + parentHandle);
driver.findElement(
By.xpath(".//*[@id='page-content']/table/tbody/tr[1]/td[1]/a"))
.click();
// get all the window handles and assign it to a set
// It will include child window and parentwindow
Set<String> windowHandles = driver.getWindowHandles();
System.out.println("No of Window Handles: " + windowHandles.size());
// remove the parent handle from the set
windowHandles.remove(parentHandle);
// the set will contain only the child window now. Switch to child
// window and close it.
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
System.out.println("Child Handle : " + winHandle);
driver.close();
}
// get the window handles again, print the size
windowHandles = driver.getWindowHandles();
System.out.println("No of Window Handles: " + windowHandles.size());
for (String winHandldes : windowHandles) {
System.out.println("Active Window : " + winHandldes);
}
driver.switchTo().window(parentHandle);
}
回答by Andy Tilston
I managed to resolve this issue by using the steps outlined by Subh above. Especially, ensure that 'Enable Protected Mode' is disabled on the 'Security' tab of 'Internet Options'
我通过使用上面 Subh 概述的步骤设法解决了这个问题。特别是,确保在“Internet 选项”的“安全”选项卡上禁用“启用保护模式”