Java:窗口处理期间焦点不在弹出窗口上 [英] Java: focus is not on pop-window during window handling
本文介绍了Java:窗口处理期间焦点不在弹出窗口上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已打开网站并应用登录,然后弹出窗口打开,我想从弹出窗口中单击,但无法打开弹出窗口.
I have opened the website and applied the Login then popup window opens, i want to click from window popup but i am not able to switch on popup.
driver.get("https://hdfcbank.com/");
driver.findElement(By.id("loginsubmit")).click();
String loginWindow = driver.getWindowHandle();
driver.switchTo().window(loginWindow);
driver.findElement(By.xpath("//*[@id='wrapper']/div[6]/a/img")).click();
我无法点击第 5 行的弹出元素.你能检查一下代码吗.
I am not able click on popup element at line 5. can you check the code.
推荐答案
检查类似问题的已接受答案
Check the accepted answer for a similar question
如何在 Selenium WebDriver 中处理弹出窗口使用Java
你需要 - getWindowHandles - &然后迭代它们.
You need to - getWindowHandles - & then iterate over them.
如果您还没有弄清楚,这里是可行的解决方案(这是针对 HDFC 示例)...
Here is the working solution in case you still haven't figured it out (this is for the HDFC example)...
String test_URL = "http://www.hdfcbank.com/";
String css_login = "img#loginsubmit";
String css_popup_continue = "img[alt='Continue']";
browser = new FirefoxDriver();
browser.navigate().to(test_URL);
List<WebElement> objLogin = browser.findElements(By.cssSelector(css_login));
if (objLogin.size() > 0) {
objLogin.get(0).click();
String parentWindowHandle = browser.getWindowHandle(); // save the current window handle.
WebDriver popup = null;
Iterator<String> windowIterator = browser.getWindowHandles().iterator();
while(windowIterator.hasNext()) {
String windowHandle = windowIterator.next();
popup = browser.switchTo().window(windowHandle);
if (popup.getTitle().contains("NetBanking")) {
List<WebElement> objPopupElement = popup.findElements(By.cssSelector(css_popup_continue));
if(objPopupElement.size() > 0){
System.out.println("Switched to Popup and found element...");
objPopupElement.get(0).click();
//Do any other operations...
break;
}
}
}
//always safe to switch back to parent window to avoid any null pointers, unless parent process got closed...
browser.switchTo().window(parentWindowHandle);
}
else {
System.out.println("Logon button not found...");
}
这篇关于Java:窗口处理期间焦点不在弹出窗口上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文