简体   繁体   English

处理硒中弹出的窗口

[英]Handle Window Pop Up in Selenium

I am working with Selenium, now there is a condition: 我正在使用Selenium,现在有一个条件:

when I hit a button in my webpage a window pop up opens up. 当我点击网页上的按钮时,会弹出一个窗口。

Now I have to click a radio button (one out of two, it will work even if we send a TAB ) and then click an OK button. 现在,我必须单击一个单选按钮(即使我们发送TAB,也可以选择两个),然后单击确定按钮。 I searched in the net and got to know about " driver.getWindowHandle() ". 我在网上搜索并了解“ driver.getWindowHandle() ”。

But I don't have any idea dealing with the newly opened window popup. 但是我不知道如何处理新打开的窗口弹出窗口。 Need help in this. 在这方面需要帮助。

For switching purpose u can use enhanced for loop: 为了进行切换,您可以使用增强型for循环:

for (String winHandle : objDriver.getWindowHandles()) {
    objDriver.switchTo().window(winHandle);
}

So it will switch the control from one driver window to child windows. 因此它将控制从一个驱动程序窗口切换到子窗口。

To interact with elements on the window try to find element with whatever tool ur using and perform the required action after switching to the window . 要与窗口上的元素进行交互,请尝试使用您使用的任何工具查找元素,然后在切换到窗口后执行所需的操作。

To return back to parent window you can use the same loop or use: 要返回到父窗口,可以使用相同的循环或使用:

driver.switchTo().defaultContent();

Check my answer in this post and also read the comments to help you understand the difference between getWindowHandle() and getWindowHandles() 在这篇文章中检查我的答案,并阅读注释,以帮助您了解getWindowHandle()和getWindowHandles()之间的区别

Java: focus is not on pop-window during window handling Java:窗口处理期间,焦点不在弹出窗口上

We handled this situation using AutoItX - https://www.autoitscript.com/site/ in our Windows/IE C# project: 我们在Windows / IE C#项目中使用AutoItX- https: //www.autoitscript.com/site/处理了这种情况:

AutoItX3 autoIt = new AutoItX3();
var handle = autoIt.WinWaitActive("[window title]", "", 20);
Assert.IsTrue(handle != 0", string.Format("Was not able to find: {0}", [window title]);
autoIt.Send("{ESCAPE}"); // tab may work as well for selection

The pop up was a Windows window, and not part of IE, therefore the WebDriver didn't know about it. 弹出窗口是Windows窗口,而不是IE的一部分,因此WebDriver对此一无所知。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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