Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Frame Names in a page using Selenium WebDriver

Is there any method in Selenium WebDriver by which we can get all the Frames in a page? Just like we have method for getting all the window Handles.

driver.getWindowHandles()
like image 645
Arun Avatar asked Feb 11 '13 12:02

Arun


People also ask

How do you select frames in Selenium?

selectFrame (frame identifier) - Selenium IDE command You need to use the selectFrame command in selenium when your page contains iframes and you have to perform some action on element inside iframe. You need to provide name or id attribute of iframe element into target column.

How you will select frames from multiple frames in Selenium?

frame(int arg0); Select a frame by its (zero-based) index. That is, if a page has multiple frames (more than 1), the first frame would be at index "0", the second at index "1" and so on. Once the frame is selected or navigated , all subsequent calls on the WebDriver interface are made to that frame.


2 Answers

This is maybe what you want then:

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.
        }
    }
}

It is important however to remind that if your page has too many of these objects, the code may become a little slower, but im talking about over 100+ in my tests when using this solution.

like image 131
aimbire Avatar answered Nov 15 '22 09:11

aimbire


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"));
    }
like image 25
Manigandan Avatar answered Nov 15 '22 08:11

Manigandan