selenium-webdriver Switching Frames Switch to a frame using C#


Example

1. Switch to a frame by Index.

Here we are switching to index 1. Index refers to the order of frames on the page. This should be used as a last resort, as frame id or names are much more reliable.

driver.SwitchTo().Frame(1);

2. Switch to a frame by Name

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

3. Switch to a frame by Title, Id, or others by passing IWebElement

If you want to switch to a frame by id or title you have to pass in a web element as a parameter:

driver.SwitchTo().Frame(driver.FindElement(By.Id("ID_OF_FRAME")));
driver.SwitchTo().Frame(driver.FindElement(By.CssSelector("iframe[title='Title_of_Frame']")));

Also note that if your frame takes a few seconds to come up, you may have to use a wait:

new WebDriverWait(driver, TimeSpan.FromSeconds(10))
    .Until(ExpectedConditions.ElementIsVisible(By.Id("Id_Of_Frame")));

Get out of a frame:

driver.SwitchTo().DefaultContent()