如何使用Java处理Selenium WebDriver中的弹出窗口 [英] How to handle Pop-up in Selenium WebDriver using Java
本文介绍了如何使用Java处理Selenium WebDriver中的弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在rediff.com中处理登录部分,但以下代码不适用于此:
I want to handle sign-in part in rediff.com, but the below code doesn't work for that:
driver.get("http://www.rediff.com/");
WebElement sign = driver.findElement(By.xpath("//html/body/div[3]/div[3]/span[4]/span/a"));
sign.click();
String myWindowHandle = driver.getWindowHandle();
driver.switchTo().window(myWindowHandle);
WebElement email_id= driver.findElement(By.xpath("//*[@id='signin_info']/a[1]"));
email_id.sendKeys("hi");
如果 myWindowHandle
不是正确的字符串,然后让我知道如何获取弹出窗口名称,因为我找不到弹出窗口的名称。
If myWindowHandle
is not the correct string, then let me know how to get the pop-up Window name, because I can't find the name of the pop-up window.
推荐答案
要切换到弹出窗口,您需要使用 getWindowHandles()
并迭代它们。
To switch to a popup window, you need to use getWindowHandles()
and iterate through them.
在您的代码中,您使用的是 getWindowHandle()
,它将为您提供父窗口。
In your code you are using getWindowHandle()
which will give you the parent window itself.
String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String subWindowHandler = null;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler); // switch to popup window
// Now you are in the popup window, perform necessary actions here
driver.switchTo().window(parentWindowHandler); // switch back to parent window
这篇关于如何使用Java处理Selenium WebDriver中的弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文