Unable to Select Element inside an Iframe

2,424 views
Skip to first unread message

Mike Hill

unread,
Sep 20, 2012, 6:40:13 AM9/20/12
to seleniu...@googlegroups.com
I've been trying to select an element inside and iframe. The iframe has the id of documentIFrame.

I switch to the iframe using this code
driver.SwitchTo().Frame("documentIFrame");

then i try and find the field using its id of HasRestrictedDemographics using this code
driver.FindElement(By.Id("HasRestrictedDemographics"));

i've tried all different ways i.e Xpath, Css etc etc.

I cant see why its not working.

Thanks

jeevan

unread,
Sep 20, 2012, 6:54:48 AM9/20/12
to seleniu...@googlegroups.com
Error stack and HTML code please.....

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/Vo-caftfMZgJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Peter Gale

unread,
Sep 20, 2012, 6:57:08 AM9/20/12
to Selenium Users
Talking about content editable iFrams, this page: http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions

... suggests using a driver.switchTo().activeElement() command.

This action seemed to fix a different iFrame issue in another post yesterday, though I have no idea why it did that.

It might be worth trying it in your page.


Date: Thu, 20 Sep 2012 03:40:13 -0700
From: mi...@hill-web.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] Unable to Select Element inside an Iframe

Mike Hill

unread,
Sep 20, 2012, 7:16:00 AM9/20/12
to seleniu...@googlegroups.com
Hi, i've read the information and i'm trying to find a check box and check it (althought i haven't uncluded the click command).

And what HTML do you need as there is alot so i'll only post relative "bits"


Thanks

jeevan

unread,
Sep 20, 2012, 7:24:00 AM9/20/12
to seleniu...@googlegroups.com

Have you tried with the option given by Peter? If it didn't worked out then provide us the small bit of html code for the below:
Frame tag and the element you want to click...

Thanks,
Jeevan.


To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/fM5dbeGsaiwJ.

Timur Nurlygayanov

unread,
Sep 20, 2012, 6:44:33 AM9/20/12
to seleniu...@googlegroups.com
Probably in this case you have few frames on one page?

Can you plese send example of html code of page?

--

Peter Gale

unread,
Sep 20, 2012, 9:03:28 AM9/20/12
to Selenium Users
have a look at: http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions

... under "how do I type into a content editable iFrame?"


Date: Thu, 20 Sep 2012 14:44:33 +0400
Subject: Re: [selenium-users] Unable to Select Element inside an Iframe
From: tnurly...@mirantis.com
To: seleniu...@googlegroups.com

Mike Hill

unread,
Sep 20, 2012, 9:22:13 AM9/20/12
to seleniu...@googlegroups.com
Hi, I've tried peter's way, which didn't work.

The html for the iframe below:

<iframe id="documentIFrame" src="../../PatientSettings/Get/111%20111%201111" frameborder="0" width="100%" height="788.8000030517578">&lt;p&gt;Your browser does not support iframes.&lt;/p&gt;</iframe>

<input id="HasRestrictedDemographics" name="HasRestrictedDemographics" type="checkbox" value="true">

jeevan

unread,
Sep 20, 2012, 9:44:01 AM9/20/12
to seleniu...@googlegroups.com
WebElement iFrame=  driver.findElement(By.id(" documentIFrame ")); 
driver.SwitchTo().Frame(iFrame); 

and then try to find the element..

Thanks,
jeevan.


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/ppNmbSvtungJ.

Mike Hill

unread,
Sep 20, 2012, 9:59:57 AM9/20/12
to seleniu...@googlegroups.com
Hi i tried that still nothing.

Heres the stack trace:
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
at OpenQA.Selenium.By.FindElement(ISearchContext context)
at Gateway.PatientSearchResults.RestrictedDemographics() in C:\Users\mhill\Documents\Visual Studio 2010\Projects\TESTSelenium\TESTSelenium\Class1.cs:line 1175

jeevan

unread,
Sep 20, 2012, 11:42:05 AM9/20/12
to seleniu...@googlegroups.com
Ok...can you check the page source after you switch to iframe. Like:

String source = driver.getPageSource();

Verify whether element presents or not.

Thanks,
Jeevan



To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/0iBGlEGG-ioJ.

Justin Gourley

unread,
Sep 20, 2012, 2:27:59 PM9/20/12
to seleniu...@googlegroups.com
Based on the HTML you provided below, it looks like this "HasRestrictedDemographics" element is outside of the iFrame (exists below iFrame close). Therefore you will not want to select the frame, and just grab the element from the get go. Unless there is another iFrame on the page that the element falls inside of. In that case select that outer frame that the element is inside of.

-Justin

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/Vo-caftfMZgJ.

Mike Hill

unread,
Sep 21, 2012, 3:45:29 AM9/21/12
to seleniu...@googlegroups.com
Does this help more?

<iframe id="documentIFrame" width="100%" height="767.5" frameborder="0" src="../../PatientSettings/Get/111%20111%201111">
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <body>
            <table id="tblPage">
                <tbody>
                    <tr>
                        <td>
                            <div class="page" style="display: inline">
                                <div id="header">
                                    <div style="clear: both; display: block;">
                                    </div>
                                    <div id="main">
                                        <form method="post" action="/testing/Gateway/PatientSettings/Get/111 111 1111">
                                        <div class="tabs ui-tabs ui-widget ui-widget-content ui-corner-all">
                                            <div class="patSettingsField">
                                                <div id="patientNumberDiv" style="display: none;">
                                                    <div class="patSettingsLabel">
                                                        <span class="patSettingsInfo"><span>
                                                            <input id="HasRestrictedDemographics" type="checkbox" value="true" name="HasRestrictedDemographics">
                                                            <input type="hidden" value="false" name="HasRestrictedDemographics">
                                                        </span>
                                                            <div>
                                                                <div id="changesSaved" class="patSettingsChangesSaved" style="display: none;">
                                                                    Changes saved
                                                                </div>
                                                            </div>
                                                    </div>
                                        </form>
                                    </div>
                                    <div style="clear: both; display: block;">
                                    </div>
                                    <div id="footer">
                                    </div>
                        </td>
                        <td>
                    </tr>
                </tbody>
            </table>
            <div id="ui-datepicker-div" class="ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all">
            </div>
        </body>
    </html>
</iframe>
On Thursday, 20 September 2012 11:40:13 UTC+1, Mike Hill wrote:

Peter Gale

unread,
Sep 21, 2012, 4:18:14 AM9/21/12
to Selenium Users
Mike

We can't tell if it's the switching to the iFrame that is failing, or the finding an element for in the iFrame that fails.

Can you try running code to switch to the iFrame and then just run a find all elements using Xpath of "//*" ... loop through that list and output the tag name for each element.

Once we know the switching works we can try to see if/why the find element fails.

Peter


Date: Fri, 21 Sep 2012 00:45:29 -0700
From: mi...@hill-web.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] Re: Unable to Select Element inside an Iframe
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/T3uSKiF9EAwJ.

Mike Hill

unread,
Sep 21, 2012, 4:41:46 AM9/21/12
to seleniu...@googlegroups.com
I've already done something similar,

After the switch to the iframe, i've tried to click on elements outside the iframe. At that point it fails (which is what i hoped for).


On Thursday, 20 September 2012 11:40:13 UTC+1, Mike Hill wrote:

Krishnan Mahadevan

unread,
Sep 21, 2012, 4:49:14 AM9/21/12
to seleniu...@googlegroups.com
Mike,
Lets go over this one more time.

After you have switched to the iframe, have you tried finding any element inside it ? Does it fail for all elements or only this element alone ? If it fails to find your particular element alone [ viz., HasRestrictedDemographics] then perhaps some javascript/ajax call is adding that element into the dom and you are trying to find it before it is done..  This is just a guess.

So I would urge you to try this and see what happens .

driver.SwitchTo().Frame("documentIFrame");
driver.FindElement(By.Id("tblPage")).GetAttribute("innerHTML");


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/lhb9kcRTLA0J.

Mike Hill

unread,
Sep 21, 2012, 4:55:02 AM9/21/12
to seleniu...@googlegroups.com
Krishnan,

After i've switched to the iframe i have tried to find several elements. All of which it couldn't find.

I'll try your code now.

jeevan

unread,
Sep 21, 2012, 4:58:21 AM9/21/12
to seleniu...@googlegroups.com
This means switch to frame is not happening....finally we can try to get the page source after we switch to the iframe and manually search for the element.



To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/k_Z9ededWKIJ.

Mike Hill

unread,
Sep 21, 2012, 4:59:52 AM9/21/12
to seleniu...@googlegroups.com
So i've done that this is the fail message i get:

Gateway.PatientSearchResults.DocumentTest:
OpenQA.Selenium.NoSuchElementException : The element could not be found

Stack Trace:

at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
at OpenQA.Selenium.By.FindElement(ISearchContext context)
at Gateway.PatientSearchResults.DocumentTest() in C:\Users\mhill\Documents\Visual Studio 2010\Projects\TESTSelenium\TESTSelenium\Class1.cs:line 1245

The line it fails on is this:

driver.FindElement(By.Id("tblPage")).GetAttribute("innerHTML");
On Friday, 21 September 2012 09:49:44 UTC+1, Krishnan wrote:

Krishnan Mahadevan

unread,
Sep 21, 2012, 5:05:34 AM9/21/12
to seleniu...@googlegroups.com
Mike,

So that kinda confirms my hunch. You are not in the intended frame to begin with.

Are you beginning your test itself with this switching, or does your code do a switch to some other frame before ? If there are other frames wherein you might have switched, then you would need to switchto default content.

Here's a simple java program that shows that switching to frames does work on normal terms.

@Test
public void foo() {
FirefoxDriver fd = new FirefoxDriver();
fd.switchTo().frame("frame_track");
String value = fd.findElement(By.id("tracknum")).getAttribute("value");
System.out.println("value = " + value);
fd.switchTo().defaultContent();
fd.quit();
}


value = 1Z12345E1512345676
PASSED: foo



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/y9elNU-88cUJ.

Peter Gale

unread,
Sep 21, 2012, 5:06:33 AM9/21/12
to Selenium Users
So can you show us what you have done and what the output was?


Date: Fri, 21 Sep 2012 01:41:46 -0700

From: mi...@hill-web.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] Re: Unable to Select Element inside an Iframe

Mike Hill

unread,
Sep 21, 2012, 6:21:25 AM9/21/12
to seleniu...@googlegroups.com
So this is the code i used:

            driver.Navigate();
            driver.FindElement(By.Id("UserName")).SendKeys("testuser");
            driver.FindElement(By.Id("UserPWD")).SendKeys("abcdef");
            driver.FindElement(By.Id("LoginButton")).Click();
            driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
            driver.FindElement(By.Id("txtPatientNo")).SendKeys("100 833 8910");
            driver.FindElement(By.Id("btnSearch")).Click();
            driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
            driver.FindElement(By.XPath("//*[@id='phMain_recordTree_tree_main']/div/div[4]/span[2]/span[2]")).Click();
            driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
            driver.SwitchTo().Frame("documentIFrame");
            driver.FindElement(By.Id("tblPage")).GetAttribute("innerHTML");
            //driver.FindElement(By.Id("HasRestrictedDemographics")).Click();


The outcome:
Gateway.PatientSearchResults.DocumentTest:
OpenQA.Selenium.NoSuchElementException : The element could not be found

Stacktrace:
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
at OpenQA.Selenium.By.FindElement(ISearchContext context)
at Gateway.PatientSearchResults.DocumentTest()



Peter Gale

unread,
Sep 21, 2012, 6:29:08 AM9/21/12
to Selenium Users
Is that the full stacktrace? ... I'd expect an "element not found" error to tell us more, like which element it was looking for and which line of code it actually failed on.

Ok ... maybe we don't *need* to see the full stack trace, but filtering out important information doesn't help so giving the full stack trace is the best option


Date: Fri, 21 Sep 2012 03:21:25 -0700
From: mi...@hill-web.com
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] Re: Unable to Select Element inside an Iframe
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/kCEZTTy6sdUJ.

Mike Hill

unread,
Sep 21, 2012, 6:33:37 AM9/21/12
to seleniu...@googlegroups.com
Yeah i noticed that, i'll run again and see if it gives more info.


Yup ran again gives the line now :S weirdness!:
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
at OpenQA.Selenium.By.FindElement(ISearchContext context)
at Gateway.PatientSearchResults.DocumentTest() in C:\Users\mhill\Documents\Visual Studio 2010\Projects\TESTSelenium\TESTSelenium\Class1.cs:line 1245

line 1245 = driver.FindElement(By.Id("tblPage")).GetAttribute("innerHTML");

Peter Gale

unread,
Sep 21, 2012, 6:43:51 AM9/21/12
to Selenium Users
So it's clealy ok with finding the iFrame and trying to switch to it, but yes, then it can't find any elements in the frame.

I'd suggest putting an arbitary long wait after the lien:

    driver.SwitchTo().Frame("documentIFrame");

... say five seconds or so.

it may be that the next line of code is executing before the page has registered the switch to the iFrame.

If an arbitary wait wokrs, you can then change it to conditional wait .. there's a "wait for element" to be visible method soemwhere, but I can't say where it is off the top of my head.


Date: Fri, 21 Sep 2012 03:33:37 -0700
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/9iM-6QCYlWkJ.

Mike Hill

unread,
Sep 21, 2012, 6:46:46 AM9/21/12
to seleniu...@googlegroups.com
So i did a simple thread.sleep(5000); after switching to the iframe.

It still fails on the same line of code as before :S

Peter Gale

unread,
Sep 21, 2012, 6:52:28 AM9/21/12
to Selenium Users
Ok ... so it's switching to the iFrame but not registering the content. We need something to trigger the loading of the content.

I think that's what this line:
   driver.switchTo().activeElement();

as used here:
http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions#Q:_How_do_I_type_into_a_contentEditable_iframe?
... is supposed to do.

Krishnan's code ash this line:

   switchTo().defaultContent();

which I presume does a similar thing.

Can you try each of these lines after the sleep and if either makes it work, then remove the sleep and see if it still works.


Date: Fri, 21 Sep 2012 03:46:46 -0700
From: mi...@hill-web.com
To: seleniu...@googlegroups.com
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/7LDoIH6KJ00J.

Mike Hill

unread,
Sep 21, 2012, 6:56:49 AM9/21/12
to seleniu...@googlegroups.com
Still failing on the driver.FindElement(By.Id("tblPage")).GetAttribute("innerHTML");

Peter Gale

unread,
Sep 21, 2012, 7:47:43 AM9/21/12
to Selenium Users
At this stage, if neither ... driver.switchTo().activeElement();

nor ... switchTo().defaultContent();
...  helps then I'm all out of ideas.

Something other than a findElements needs to be done here, but I've no idea what, and I think we made all the known recommendations I've seen.

Could it be that something needs triggering in the AUT to activate the iFrame? Maybe the manual user's actions trigger something else were not aware of?

I can only suggest having one of the local developers who has worked on the app sit with you to try to understand what you are trying to do and see if they can make sense of what is happening in the app.



Date: Fri, 21 Sep 2012 03:56:49 -0700
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/z3dm_H41vX8J.

Mike Hill

unread,
Sep 21, 2012, 8:27:15 AM9/21/12
to seleniu...@googlegroups.com
Cheers for all your help, i'll speak to the dev's and see what happens :)

Vivektests

unread,
Aug 22, 2013, 3:18:56 AM8/22/13
to seleniu...@googlegroups.com
Mike,
Facing similar issue.

driver.switchTo().frame(1); // there is no ID or name to the iFrame 
// checked that there are only 2 iFrames 1 in the base page and the other in the popup
	
	try {
		System.out.println("Wait for the popup to get loaded");
		Thread.sleep(15000);
	} catch (InterruptedException e){
		System.out.println("Exception : " + e.getMessage());
	}
	
	driver.findElement(By.cssSelector("#tab3")).click(); // tab
	driver.findElement(By.cssSelector("#p1")).click(); // check box
	driver.findElement(By.cssSelector("#pNote")).click(); // text area
	driver.findElement(By.cssSelector("pButtonClose")).click();	// close button for the popup
	
	//switch out of the frame back to the main screen, probably this is not required, but not sure at this point
	driver.switchTo().defaultContent();
	
Error I am getting =============================================

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"#p1"}

As you can see the error is not occuring when click on #tab3 is done (note: tab3 is selected by default then the popup opens)

Not getting what I am missing here.
Tried Issues search, googling and FAQ, unfortunately stuck!

Can you please share if this got resolved for you and how.
Thanks

kanimozhi vellaisamy

unread,
Dec 15, 2013, 11:58:04 AM12/15/13
to seleniu...@googlegroups.com
Hi,

Am facing the same issue. Did anybody arrived to a solution for this. It will be much help full, if anyone shares their thought.

Thanks,
Kani.

A. Luis Lopes

unread,
Mar 21, 2015, 12:59:11 AM3/21/15
to seleniu...@googlegroups.com
Hi all,

I have the same issue, someone have the solution or any workaround for this?

Thanks

António

A. Luis Lopes

unread,
Mar 21, 2015, 12:59:11 AM3/21/15
to seleniu...@googlegroups.com
Hi all,

I solved the problem by doing 'driver.switchTo().defaultContent();' before switching to the wanted iFrame. But I was already at top frame; usually we don't need to force 'defaultContent()' when we are at top frame.

Thanks

António

On Sunday, December 15, 2013 at 4:58:04 PM UTC, kanimozhi vellaisamy wrote:
Reply all
Reply to author
Forward
0 new messages