Selenium / Java / iframe

57 views
Skip to first unread message

Elaine Leese

unread,
Feb 12, 2020, 6:32:11 PM2/12/20
to Selenium Users
Using Selenium and programming in Java, I am struggling to interact with what I believe are iframes. The troublesome page has a button that when clicked opens a new browser window. To clarify, I will refer to this new browser window as the child window and the original as the parent window. The two windows interact with each other.

I have examined the source code of both pages and the parent has three iframe tags (occasionally it has four), the child has none. I have literally tried interacting with every single ID, Class, Name, XPath, etc. on the child window and nothing has worked.

To attempt to confirm that the child window is indeed just generated by the parent window I used the getCurrentUrl. They both returned the same String.

I have tried the driver.switchTo().frame(0); command trying every frame available in a For loop, but I receive the same error that every element I try to interact with doesn't exist. I have also tried to switchTo().defaultContent and parentFrame(). The best I can do is interact with the parent window.

I am not sure if this is relevant, but the child window has the internet address about:blank (which is unchangeable), the title: Untitled, and has no Toolbar. I am using the latest version of Google Chrome.

Any help please? Is the problem iFrames?

vikas sv

unread,
Feb 17, 2020, 8:37:44 AM2/17/20
to Selenium Users
As I read your description of the issue, I see that you are not switching to the new window,
Try the below,

// Save the parent window handle
String winHandleParent = driver.getWindowHandle();

// Perform the click operation that opens child window

// Switch to child window(This will switch to the last available window)
for(String winHandle : driver.getWindowHandles()){
    driver.switchTo().window(winHandle);
}

// Perform the actions on child window

// Close the child window
driver.close();

// Switch back to parent window
driver.switchTo().window(winHandleParent);

Best Regards,
Vikas SV

Shawn McCarthy

unread,
Feb 17, 2020, 8:43:03 AM2/17/20
to Selenium Users
I don't think you can assume this is always true "// Switch to child window(This will switch to the last available window)". You might want to check that the window handle you switch to (for the child) is NOT the `winHandleParent`.

vikas sv

unread,
Feb 18, 2020, 4:36:35 AM2/18/20
to Selenium Users
If you see the code , it is in a loop, and window handles will be stored in order of window creation, so obviously the last child window will be the last available window handle. 
So by the end of the loop, child window will be selected.

Best regards,
Vikas

Jim Evans

unread,
Feb 18, 2020, 9:36:55 AM2/18/20
to Selenium Users
Your assumption that “ window handles will be stored in order of window creation” is explicitly incorrect. Window handles are returned by the getWindowHandles() method are specifically in no guaranteed order.

vikas sv

unread,
Feb 18, 2020, 9:54:48 AM2/18/20
to Selenium Users
Yes you are right, I read about it and understood that the windowhandles will not be in the same order as created,

So the below code can be used, here as Shawn commented above I am checking if it is not the parent window and switching to child window.

String parent_window = driver.getWindowHandle();

link1.click();   //Click some link to open child window

Set<String> allWindows = driver.getWindowHandles();
for(String child:allWindows){
    if(!parent_window.equalsIgnoreCase(child))
        driver.switchTo().window(child);
}

//Perform operations on child window

//Close the child window
driver.quit();

//switch to parent window
driver.switchTo().window(parent_window);


Best regards,
Vikas
Reply all
Reply to author
Forward
0 new messages