locate iframe

279 views
Skip to first unread message

Hai Quang Kim

unread,
Apr 30, 2013, 6:20:08 AM4/30/13
to co...@googlegroups.com
Hello guys,

My page has an iframe, I could just use FindFrame but there is a case that I don't have the id or anything else of the frame.
So I am using FindCss and FindFrame to locate the same element for comparison and I got the 2 similar RobustElementScope objects.
Both have the same id, name...The only difference is the Native (FindFrame with OpenQA.Selenium.Chrome.ChromeDriver vs FindCss with OpenQA.Selenium.Remote.RemoteWebElement)
When I use the above elements to get a child div, the element with the Native=ChromeDriver will return me the correct one but the element with the Native=RemoteWebElement return the wrong one (missing).

My question here is: is this considered as a bug or I am not supposed to use FindCss to find an iframe or do I need to perform switch to iframe?

i appreciate any help you can provide.



Hai Quang Kim

unread,
May 2, 2013, 5:23:05 AM5/2/13
to co...@googlegroups.com
FYI, I dive into the code and it looks like FindFrame will do a switch to but FindCss will not.

Hai Quang Kim

unread,
May 2, 2013, 6:42:47 AM5/2/13
to co...@googlegroups.com
I got more details for this:
- find an iframe using FindFrame with id will return Coypu.Drivers.Selenium.SeleniumFrame which have 
        public override object Native
        {
            get
            {
                selenium.SwitchTo().Frame(NativeSeleniumElement);
                return selenium;
            }
        }
- find an iframe using FindCss("iframe") will return SeleniumElement which have
        public virtual object Native
        {
            get { return native; }
        }

I guess it should work if you have iframe with id,title, name ..
I am just unlucky to have the page that has the iframe without any of good info to be located. 
I hope this will help :)

Adrian Longley

unread,
May 2, 2013, 7:07:27 AM5/2/13
to co...@googlegroups.com
Hi, yes you are quite right about all this.

I suppose FindCss could look at the type of element it has found and if it is an iframe then treat it the same way as FindFrame. Alternately I suppose we could make it possible to pass a CSS selector to FindFrame, but then I suppose we ought to do that everywhere for consistency.

How does the first option sound - that way it would just have worked for you? It's a pretty simple change.

To work around in the meantime you could always call into selenium by casting the BrowserSession.Native to IWebDriver and doing the switching yourself like in the code you reference below, but this would be quite ugly and you'd have to manage switching back and forth which is hard and Coypu does this for you normally.

Assuming you can't change the markup, probably the easiest short-term hack (so long as you don't need to test with noscript) would be to hack an id onto the element with some javascript like so:

  var tempId = Guid.NewGuid().ToString();
  browserSession.ExecuteScript("document.querySelector('iframe.my-class-name').id = '" + tempId + "';");
  var myFrame = browserSession.FindFrame(tempId);

I will do what I suggested though so that it just works with FindCss and FindId and reply to this thread when it's ready.
  
Cheers,
Adrian
 

--
You received this message because you are subscribed to the Google Groups "Coypu" group.
To unsubscribe from this group and stop receiving emails from it, send an email to coypu+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Hai Quang Kim

unread,
May 3, 2013, 10:09:37 AM5/3/13
to co...@googlegroups.com
Finally I got it working, i change the code so that in SeleniumElement the Native will check for the TagName and it will do a switch to for iframe. but the solution is not as nice as I expected.
I am just thinking maybe we still need to expose switchto functionality so that the API is flexible, just thinking.
Cheers,

Adrian Longley

unread,
May 3, 2013, 10:15:52 AM5/3/13
to co...@googlegroups.com
On 3 May 2013, at 15:09, Hai Quang Kim wrote:

Finally I got it working, i change the code so that in SeleniumElement the Native will check for the TagName and it will do a switch to for iframe. but the solution is not as nice as I expected.

Why not? I tried the same last night and it seemed fine.
Reply all
Reply to author
Forward
0 new messages