can't find element in new opened panel

2,183 views
Skip to first unread message

sandy

unread,
May 25, 2011, 4:10:47 AM5/25/11
to Selenium Users
I have a link on the page.
<a href="javascript:nd();popupConfigDialog('');">Monitor Groups</a>

Click the link would call the following js method.
function popupConfigDialog(link, width, height) {
if (link.indexOf("/support") != -1) {
popupFull(link, "support", width, height, true, false, true, false);
} else {
popupDialog(link, "TopLevelConfiguration", window, width,
height, true, true);
}
}

The abve js method would call another js method in a jsp file to form
a new window.
function popupDialog(param_href, name, arg, param_width, param_height,
param_hasScroll, param_resize)
{
......
dialogBlock = new StringBuffer();
dialogBlock.append('<html><head><script src="/dialogs.jsp" type="text/
javascript"></script><script src="/dragging.jsp" type="text/
javascript"></script></head>');
dialogBlock.append('<body style="margin: 5px 0 0 0;');
dialogBlock.append('background-image: url(/images/
HeaderDialogLine.gif);');
dialogBlock.append('background-repeat: repeat-x; overflow: hidden;"
onmousedown="beginDrag(event);" onmouseup="endDrag()">');
dialogBlock.append('<div id="');
dialogBlock.append(DIALOG_FRAME_TITLE_DIV_NAME + dialogNumber +
'">');
dialogBlock.append('<img onclick="closeDialog();" ');
dialogBlock.append(' onmouseover="highlightCloseIcon(this, true)"
onmouseout="highlightCloseIcon(this, false)" ');
dialogBlock.append('style="z-index:10003; float:right; margin-right:
10px; padding-top: 0px; cursor: pointer;" src="/images/x-off.gif" ');
dialogBlock.append('title="<
%=rb_core.getString("CLICK_TO_CLOSE_DIALOG")%>"/>');
dialogBlock.append('<span style="color: #000000; font-family:
verdana; font-size: 0.9em; font-weight: bold; ');
dialogBlock.append('padding-left: 5px" id="');
dialogBlock.append(DIALOG_FRAME_TITLE_NAME + dialogNumber);
dialogBlock.append('"></span>');
dialogBlock.append('</div></body></html>');

//create resizable dialog
ResizableContainer.init(insertToWindow, dialogNumber, width, height);
.....
}

Now I use web driver and the window can be opened but I can't get the
element in the new opened window.

driver.findElement(By.linkText("Monitor Groups")).click(); //Click
the link
for (String handle : driver.getWindowHandles()) {

System.out.println(driver.getWindowHandles().size()); //
I get the size is 1

System.out.println(driver.switchTo().window(handle).getTitle()); //
The tile is previous page's.

driver.findElement(By.id("newMonitorGroup")); //
Find element in the new window but an exception would happen.
}

The exception says Unable to locate element:
{"method":"id","selector":"newMonitorGroup"}

Please give me some guidance, thanks very much.

Luke Inman-Semerau

unread,
Jun 6, 2011, 7:50:01 PM6/6/11
to seleniu...@googlegroups.com
You need to wait for the new window to appear, try this:

final Set<String> handles = driver.getWindowHandles();

driver.findElement(By.linkText("Monitor Groups")).click(); 

// It wasn't waiting for the popup to be loaded... bug in webdriver maybe? I'll have to ask....
String newWindow = (new WebDriverWait(driver, 10)).until(new ExpectedCondition<String>() {
public String apply(WebDriver input) {
Set<String> newHandles = input.getWindowHandles();
newHandles.removeAll(handles);
if (newHandles.size() > 0) {
return newHandles.iterator().next();
}
return null;
}
});

driver.switchTo().window(newWindow);

// wait for the element to appear
WebElement element = (new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>() {
public WebElement apply(WebDriver input) {
return input.findElement(By.id("newMonitorGroup"));
}
});


This assumes that clicking the link will actually open a popup (new window) and that new window will have an ID "newMonitorGroup" inside of it.


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.


Chandler Chen

unread,
Jan 16, 2014, 2:18:32 AM1/16/14
to seleniu...@googlegroups.com
Hi Luke, thanks for your solution and it works for me.

Furthermore, under some edge condition, the found handle of new window will be empty, so I add below protections based on your solution.

final Set<String> handles = driver.getWindowHandles();

driver.findElement(By.linkText("Monitor Groups")).click(); 

// It wasn't waiting for the popup to be loaded... bug in webdriver maybe? I'll have to ask....
String newWindow = (new WebDriverWait(driver, 10)).until(new ExpectedCondition<String>() {
public String apply(WebDriver input) {
Set<String> newHandles = input.getWindowHandles();
newHandles.removeAll(handles);
if (newHandles.size() > 0) {
                                if(!"".equals(tmpHandle)) {
    return newHandles.iterator().next();
    }
}
return null;
}
});

driver.switchTo().window(newWindow);

在 2011年6月7日星期二UTC+8上午7时50分01秒,Luke Inman-Semerau写道:
Reply all
Reply to author
Forward
0 new messages