Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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 selenium.click("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 selenium.click("//span[text()='Link']") but it doesn't seem to do anything. Any ideas, please?

Here is the code:

selenium.click("//span[text()='tab that loads iframe']");   

Thread.sleep(5000);   

selenium.selectFrame("tabContextFrame");         

selenium.mouseOver("//span[text()='Link']");   

selenium.mouseDown("//span[text()='Link']");  

selenium.mouseUp("//span[text()='Link']");   

Thread.sleep(5000);   

selenium.selectFrame("null"); 

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.

driver.switchTo().frame("foo");

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):

driver.switchTo().defaultContent();

You can also refer to this link: https://github.com/prabhpreetk/selenium-google-code-issue-archive.git

Browse Categories

...