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()
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.
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.
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.
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"));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With