Switching between nested iframes within a frame

5,329 views
Skip to first unread message

bhaskarvphani

unread,
Mar 31, 2012, 5:13:39 PM3/31/12
to webdriver
Please help me in resolving the following issue.

Hierarchy of Frameset, frames and iframes in a single web page

<Frameset id=”mainframeset”….>
<frame id=”usersession”…>
<Frameset id=”lowerframeset”…>
<Frame id=”Nav”…>
<Frameset =”main pane”…>
<Frame id=”Work”…>
<Iframe1> (each Iframe have attributes
frameborder="0",allowtransparency="true",tabindex="0",src="",title="Rich
text editor, work_135(only change is this id for each
iframe), press ALT 0 for
help.",style="width:100%;height:100%"
<Iframe2> (each Iframe have attributes
frameborder="0",
allowtransparency="true",tabindex="0",src="",title="Rich text editor,
work_140(only change is this id for each
iframe), press ALT 0 for
help.",style="width:100%;height:100%"
<Iframe3>
<Iframe4>

Code I have is :

driver.switchTo().defaultContent();
driver.switchTo().frame("work");
System.out.println(" Switched to Work ");
List<WebElement> Element = driver.findElements(By.xpath("//iframe"));
System.out.println("No of Iframes are :"+ Element.size());
System.out.println(driver.switchTo().frame(Element.get(0)));
driver.findElement(By.xpath("//*[@id='ck_article_title']")).click();
driver.findElement(By.xpath("//*[@id='ck_article_title']/
p[1]")).sendKeys("Ifram 1 ");

System.out.println(driver.switchTo().frame(Element.get(1)));
driver.findElement(By.xpath("//
*[@id='ck_article_standfirst']")).click();
driver.findElement(By.xpath("//*[@id='ck_article_standfirst']/
p[1]")).sendKeys("I frame 2 ");

Error while running the above code:

Exception in thread "main"
org.openqa.selenium.StaleElementReferenceException: Element belongs to
a different frame than the current one - switch to its containing
frame to use it

Problem:

a) The above code enters the text in iframe 1 but gives above error
when trying to identify iframe2 within frame "work"

Please help me to identify each iframes sequentially and enter
required text in it.

Luke Inman-Semerau

unread,
Apr 2, 2012, 7:05:35 AM4/2/12
to webd...@googlegroups.com
Before switching to another iframe you need to add these lines:

driver.switchTo().defaultContent();
driver.switchTo().frame("work");

Switching only goes down the chain, if you need a sibling frame you have to go back to the top frame and go back down again.

-Luke

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

Bhaskar Phani

unread,
Apr 2, 2012, 7:37:59 AM4/2/12
to webd...@googlegroups.com
Luke,

After the post i have modified to code as you mentioned , the switch to iframe2 is happening but Xpath is not found for Iframe 2

but when checking Xpath individually its working.

Please fin below the modified code:

if ("Polopoly CM".equals(driver.getTitle())) {
System.out.println("----> Title of page  <----");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.switchTo().frame("nav");
driver.findElement(By.xpath("//*[starts-with(@id,'nav_') and @class='p_textOutput']")).click();
   System.out.println(" Nav click ");
    
   driver.switchTo().defaultContent();
   driver.switchTo().frame("work");
System.out.println(" Switched to Work ");
System.out.println("Intial Article Title Iframe Tag Name"+driver.findElements(By.tagName("iframe")).get(0));
System.out.println("Intiatl Standfirst  Iframe Tag Name"+driver.findElements(By.tagName("iframe")).get(1));
System.out.println("Intial Body Text Iframe Tag Name"+driver.findElements(By.tagName("iframe")).get(2));
System.out.println("Intial Editorial Notes Iframe Tag Name"+driver.findElements(By.tagName("iframe")).get(3));
// Article Title
System.out.println("Article Title Iframe Tag Name"+driver.findElements(By.tagName("iframe")).get(0));
driver.switchTo().frame(driver.findElements(By.tagName("iframe")).get(0));
driver.findElement(By.xpath("//*[@id='ck_article_title']")).click();
driver.findElement(By.xpath("//*[@id='ck_article_title']/p[1]")).sendKeys("Article Title for Automation ");
System.out.println("default content frame"+driver.switchTo().defaultContent().getTitle());
System.out.println("Switch to Work"+driver.switchTo().frame("work"));
// Standfirst
System.out.println("Standfirst Iframe Tag Name :"+" "+driver.findElements(By.xpath("//iframe")).get(1));
driver.switchTo().frame(driver.findElements(By.tagName("iframe")).get(1));
System.out.println("Switch is done for standfirst");
driver.findElement(By.xpath("//*[@id='ck_article_standfirst']")).click();
driver.findElement(By.xpath("//*[@id='ck_article_standfirst']/p[1]")).sendKeys("Article Standfirst for Automation ");
        System.out.println("default content frame"+driver.switchTo().defaultContent().getTitle());
System.out.println("Switch to Work"+driver.switchTo().frame("work"));
}

Error: Unable to locate element: {"method":"xpath","selector":"//*[@id='ck_article_standfirst']/p[1]"}

Thanks
Bhaskar
--
Thanks and Regards
Bhaskar
Mob:07582604105

Luke Inman-Semerau

unread,
Apr 2, 2012, 7:54:00 AM4/2/12
to webd...@googlegroups.com
Looks like you're clicking something right beforehand, likely a timing issue and you need to add a wait. 


btw your xpath usage is a bit excessive, try some CSS selectors too ;)

-Luke

Bhaskar Phani

unread,
Apr 2, 2012, 7:56:38 AM4/2/12
to webd...@googlegroups.com
Luke,

I used implicit wait statement. No change

Will try with Css selectors and let you know.

Thanks for the solutions.
Bhaskar

darrell

unread,
Apr 2, 2012, 8:25:39 AM4/2/12
to webd...@googlegroups.com
Try to visualize the frames as a tree. The main window would be the root of the tree. If the main window contains 2 frames then you have 2 branches from root to the 2 frames. If the first frame contains 3 frames then you have 3 branches from the first frame. To get to one  of the '3' frames you would have to switch from the main window to the first frame then from the first frame to the nested frame. If you needed to get to the second frame (from the main window) then you have to switch to the main window then down to the second frame. If your code jumps across branches then it will fail.

I find visualizing the nesting using branches and a tree diagram makes things a lot easier for me.

Darrell
Bhaskar

> To unsubscribe from this group, send email to webdriver+unsubscribe@googlegroups.com.

> For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.
>

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.




--
Thanks and Regards
Bhaskar
Mob:07582604105

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

Bhaskar Phani

unread,
Apr 2, 2012, 8:34:01 AM4/2/12
to webd...@googlegroups.com
Darell,

This is the structure i have visualized, 

<Frameset id=”mainframeset”…./>
    <frame id=”usersession”…/>
       <Frameset id=”lowerframeset”.. />    
           <Frame id=”Nav”/>
           <Frameset =”main pane”…/>  
                   <Frame id=”Work”…/>  
                           <Iframe1 :/>  (Article Title)
                           <Iframe2: />   (Article Stand first)
                           <Iframe3:/>    (Article Body Text)  
                           <Iframe4: />  (Article Editorial Notes)

Switching in my code happens from Default Content -> Work-> IFrame 1 (Element with given Xpath is working)

This is working fine but on running like below Xpath in Iframe 2 is not found

Default Content -> Work-> IFrame 1 -> Default Content -> Work-> Iframe 2 

Switch to Iframe 2 is successful but not finding element in iframe 2 through XPath

This is where i got stuck.

Thanks
Bhaskar

To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/4_uKDLEGKBUJ.

To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

Luke Inman-Semerau

unread,
Apr 2, 2012, 11:04:19 AM4/2/12
to webd...@googlegroups.com
No one will be able to help you futher unless you provide all the HTML (and JavaScript) of your site or a link to a public facing version of it. 

I'll stand by my guess that your click is updating the page somehow and that's why you can't locate your next element (or that element is in another frame entirely). 

-Luke
Reply all
Reply to author
Forward
0 new messages