Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwitchTo() Frame in Selenium with c#

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

like image 878
EHU Avatar asked Dec 11 '22 02:12

EHU


2 Answers

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')]")));
like image 107
Subh Avatar answered Dec 22 '22 02:12

Subh


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();

like image 45
Hemlata Gehlot Avatar answered Dec 22 '22 02:12

Hemlata Gehlot