How to get frame names on a page using Selenium WebDriver

Is there any method in Selenium WebDriver with which we can get all the frames on the page? Just as we have a method for getting all window handles.

driver.getWindowHandles()
+5
source share
2 answers

Perhaps this is what you want:

public void getIframe(final WebDriver driver, final String id) {
    final List<WebElement> iframes = driver.findElements(By.tagName("iframe"));
    for (WebElement iframe : iframes) {
        if (iframe.getAttribute("id").equals(id)) {
        // TODO your stuff.
        }
    }
}

However, it is important to remember that if there are too many such objects on your page, the code may become a little slower, but I'm talking about more than 100+ in my tests using this solution.

+6
source

Try this code:

    //Assume driver is initialized properly. 
    List<WebElement> ele = driver.findElements(By.tagName("frame"));
    System.out.println("Number of frames in a page :" + ele.size());
    for(WebElement el : ele){
      //Returns the Id of a frame.
        System.out.println("Frame Id :" + el.getAttribute("id"));
      //Returns the Name of a frame.
        System.out.println("Frame name :" + el.getAttribute("name"));
    }
+3
source

All Articles