SwitchTo() Frame in Selenium with c#

EHU picture EHU · Dec 12, 2014 · Viewed 17.1k times · Source

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

Answer

Subh picture Subh · Dec 12, 2014

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')]")));