xpath with iframes

5,486 views
Skip to first unread message

LearningSelenium

unread,
Dec 9, 2013, 3:35:01 PM12/9/13
to webd...@googlegroups.com

I am using chrome development tool to find working xpath for the elements to use in webdriver.

I am facing the same issue. the descendant word doesn't work.The XML looks like in case of iframes

<iframe id="..." name="..." title="..." src="..." onLoad="...">
 #document
    <!DOCTYPE html>
     <html dir="..">
      <head>
       <body...>
       ..
        <div class="xxx">`enter code here`
    </html>

So when i try to execute the xpath $x("//iframe[@id='abcdFrame']") it gives me iframe successfully.

but when i try to execute $x("//iframe[@id='abcdFrame']//html") 

or

$x("//iframe[@id='abcdFrame']/descendent::html") 

or

$x("//iframe[@id='abcdFrame']/descendant::node()/html")

it just returns null. Can anybody please 'help..

Thanks.

taru

unread,
Dec 9, 2013, 4:59:29 PM12/9/13
to webd...@googlegroups.com
Hi,

Webdriver is very picky when it comes to frames/iframes.

you have switch to frame to access the elements within the frame, use following to locate all the available frames on the page, get their names(or ids)

List<WebElement> frameset = driver.findElements(By.tagName("iframe"));      
            for (WebElement framename : frameset)
                logger.info("frameid: " + framename.getAttribute("name"));               
           
then switch to the frame you want

driver.switchTo().frame("frameName"); //or frameid
once you are done with frame, switch back to default content 

driver.switchTo().defaultContent();
hope this helps.

thx

darrell

unread,
Dec 10, 2013, 7:09:28 AM12/10/13
to webd...@googlegroups.com
Have a read of http://darrellgrainger.blogspot.com/2012/04/frames-and-webdriver.html. You have to treat iframes as if you are switching to a different page. Just like I might have a page with a link, I click the link and it goes to a new page. With frames, I have a page with an iframe, I use switchTo() to switch to the frame and now everything acts like I navigated to the new 'page', i.e. frame.
Reply all
Reply to author
Forward
0 new messages