Selenium: Unable to access iframe and data inside it

SwitchTo() method takes index, name or frame element, so you can try use name or frame element.

//find the frame by class/title/type/source
IWebElement detailFrame = driver.FindElement(By.XPath("//iframe[@class="class1"]"));
driver.SwitchTo().Frame(detailFrame);

//alternatively, find the frame first, then use GetAttribute() to get frame name
IWebElement detailFrame = driver.FindElement(By.XPath("//iframe[@class="class1"]"));
driver.SwitchTo().Frame(detailFrame.GetAttribute("name"));

//you are now in iframe "Details", then find the elements you want in the frame now
IWebElement foo = driver.FindElement(By.Id("foo"));
foo.Click();

//switch back to main frame
driver.SwitchTo().DefaultContent();

Leave a Comment