Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium and iframe

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 a 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");  
like image 507
stumped Avatar asked Sep 27 '11 21:09

stumped


People also ask

Does Selenium work with iframe?

For a browser to automatically start interacting with the web page, it is essential for the browser to identify the elements under the frame for Selenium testing. It is possible to identify the iframes on a web page in two ways: Right-click on the specific element and check all the options.

What is difference between frame and iframe in Selenium?

Difference between Frame and iFrame in SeleniumA frame is used to divide a page into multiple sections, with new content on each section. An iFrame is used to embed the content of the external websites into the web page, in order to avoid cross-site scripting issues.

How do I click iframe in Selenium?

Right click on the element, If you find the option like 'This Frame' then it is an iframe. (Please refer the above diagram) Right click on the page and click 'View Page Source' and Search with the 'iframe', if you can find any tag name with the 'iframe' then it is meaning to say the page consisting an iframe.

How does Selenium handle multiple iframes?

To work with different nested iframes, we can use the method switchTo() to switch the frames using Index, Name, or Id & Web Element. Moreover, you can switch to immediate parent iframe using method switchTo(). parentFrame(). You can switch to the main web page from child iframe using method switchTo().


1 Answers

I'm guessing you are using Selenium 1.0. Have you looked at Selenium 2.0 and WebDriver. I found the following and it worked for me:

Q: How do I type into a contentEditable iframe? A: Assuming that the iframe is named "foo":

driver.switchTo().frame("foo");
WebElement editable = driver.switchTo().activeElement(); 
editable.sendKeys("Your text here"); 

Sometimes this doesn't work, and this is because the iframe doesn't have any content. On Firefox you can execute the following before "sendKeys":

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

This is needed because the iframe has no content by default: there's nothing to send keyboard input to. This method 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 with this specific frame):

driver.switchTo().defaultContent();

I found this on http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions

like image 68
Dan Woodward Avatar answered Nov 16 '22 09:11

Dan Woodward