Working with iframes in Selenium Webdriver
- qatestingthreesixt
- Dec 1, 2021
- 1 min read
Suppose you have html like this:

If iframe has name or id, then use: driver.switchTo().frame(name_or_id)
If page contains only one or two iframes then we can use this method: driver.switchTo().frame(index) or If you have only one iframe in the page, try driver.switchTo().frame(0)
You can also switch to iframe by finding its CSS/Xpath/id. Syntax is: driver.switchTo().frame(iframe_element) By using cssSelector:
driver.switchTo().frame(driver.findElement(By.cssSelector(“iframe[src=’path of iframe”)));
driver.switchTo().frame(driver.findElement(By.cssSelector(“iframe[title=’title of the iframe’]”)));
driver.switchTo().frame(driver.findElement(By.tagName(“iframe”)));
Comments