SwitchTo() Frame in Selenium with c#
Asked Answered
G

3

5

I use selenium on this site, but I cant use any of its elements. Because they comes from 'frame' and its in 'frameset', here is html part;

 <frameset rows="0%, 95%" frameborder="0" frameSpacing="0" marginHeight="0" marginWidth="0">
                <frame id='unloadFrame' src="/somesrc" noresize>
                <frame src="/somesrc" noresize>
 </frameset>
            <noframes>
              Your browser doesn't support frames, This web site requires a frames capable browser.
            </noframes>

I need to access second frame which starts with src, I used this method but still cant use any element;

driver.SwitchTo().Frame(1);
Boolean sa = driver.FindElements(By.Id("ctl00_MainContent_txtCustomerId")).Count > 0;
if (sa == true)
{
    driver.FindElement(By.Id("ctl00_MainContent_txtCustomerId")).SendKeys("HelloWorld");
}

Is it because Frame(1) is not frame i want ? Or should i use different way to get into it?

Thank You

Gait answered 12/12, 2014 at 15:27 Comment(4)
Are you sure there are no more frames on a page?Interject
The syntax for switching to a frame looks correct. Are you sure element with id="ctl00_MainContent_txtCustomerId" exists in the frame?Interject
Well i try different way to write it(maybe master page prefix) but no resultGait
possible duplicate of Unable to find element in Selenium WebDriver By Name and XPathRumery
C
10

This code will also switch to the frame with 'src' attribute as '/somesrc' and with 'id' attribute not 'unloadFrame'

driver.SwitchTo().Frame(driver.FindElement(By.Xpath("//frame[@src='/somesrc' and not(@id='unloadFrame')]")));
Cathrin answered 12/12, 2014 at 15:39 Comment(3)
This is exacly what i am looking for. Thank You sirGait
Nope, it is cheating (joking, +1) :) Then, why driver.SwitchTo().Frame(1); hasn't worked?Interject
:D Well, I also don't know. Only the OP can answer how the frame(s) are implemented in his/her app which may clear the cloud as to why driver.SwitchTo().Frame(1); didn't work. I merely provided an alternative.. I also have a hunch that driver.SwitchTo().Frame(0); might work here.. Can't say for sure though, but that id=unloadframe is ticking me.. :)Cathrin
H
2

I am writing this simple method to enter text in Iframe.

public void SwitchToIframeandEnterText(IWebElement frame, 
                                     IWebElement field, string txt)
    {
        Driver.SwitchTo().Frame(frame);
        field.SendKeys(txt);
        Driver.SwitchTo().DefaultContent();
    }

Where,

  • IWebElement frame = Locator of Iframe
  • IWebElement field = Locator of Field where you want to enter text
  • string txt = text that you want to enter
Hughmanick answered 25/5, 2018 at 10:34 Comment(0)
A
2

Switch IFrame in C# -

  1. Index
  2. ID or Name
  3. Web Element

eg-

  1. driver.SwitchTo().Frame(0);

  2. driver.SwitchTo().DefaultContent();

  3. IWebElement element= driver.FindElement(By.Id("ElementID"));

  4. driver.SwitchTo().ParentFrame();

Arneson answered 5/7, 2018 at 10:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.