Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.7k points)

I have an iframe that gets loaded when I click on a tab on a page. When I use Firebug to look at the iframe on IE8, all I see is:

iframe id=tabContextFrame class=contextFrame contentEditable=inherit src=/xyz.dt?forward=show&layouttype=NoHeader&runid=1234  name=tabContextFrame url=/xyz.dt?forward=show&layouttype=NoHeader&runid=1234  scrolling=auto

and that's it. The hierarchy below the iframe can't be seen. I want to click on a link within the iframe. To find the elements within the iframe, I did a"on the tab that loads the iframe") and then selenium.getHtmlSource(). From this source, I can at least locate my link of interest. I did"//span[text()='Link']") but it doesn't seem to do anything. Any ideas, please?

Here is the code:"//span[text()='tab that loads iframe']");   








1 Answer

0 votes
by (62.9k points)

I'm assuming you are using Selenium 1.0. Have you looked at Selenium 2.0 and WebDriver? I found the following and it worked for me. I am assuming that the iframe is named "foo", which will be used to perform typing into the contentEditable iframe.


WebElement editable = driver.switchTo().activeElement(); 

editable.sendKeys("Your text here"); 

Many a time, this may not work, and this is because the iframe doesn't have any content. However, on Firefox you can perform the following before "sendKeys" method:

((JavascriptExecutor) driver).executeScript("document.body = '<br>'"); 

This is required because the iframe has no content by default, which means there's basically nothing to send the keyboard input to. This function call inserts an empty tag, which sets everything up nicely.

Remember to switch out of the frame once you're done (as all further interactions will be within this particular frame):


You can also refer to this link:

Browse Categories