Get all Frame Names on the Page

02 May, 2014
Frame in HTML defines a particular area in which another HTML document can be displayed. HTML frames allow to present documents in multiple views, which may be independent windows or subwindows. Multiple views offer designers a way to keep certain information visible, while other views are scrolled or replaced.
iframe element is preferred to frame element due to performance problems and few other reasons.

To get a list of all the frame names available on the page use the below code:

//Make sure you are in default frame
driver.switchTo().defaultContent();
//change 'frame' to 'iframe' if you need to find iframes
List framesetList=driver.findElements(By.tagName("frame")); 
if(framesetList.size()>0){
     for(WebElement framename :framesetList){
      System.out.println("Frame name found:" + framename.getAttribute("name"));
     }
}
else {
       System.out.println("No frame found");
}

If you want to find a particular frame then use something like below:
public void getFrame(final WebDriver driver, final String id) {
    //change 'frame' to 'iframe' if you need to find iframes
    final List frames = driver.findElements(By.tagName("frame")); 
    for (WebElement frame : frames) {
        if (frame.getAttribute("id").equals(id)) {
        // TODO your stuff..
        }
    }
}

No comments:

Post a Comment