1

Snapshot of Frame_top with 3 nested frames:

Frame_top with 3 nested frames

Code:

WebElement topframe = driver.findElement(By.xpath("//frame[@name='frame-top']"));   
String frame1 =  driver.switchTo().frame(topframe).switchTo().frame("frame-left").findElement(By.xpath("//body")).getText();                
System.out.println(frame1);
List<WebElement> nestedFrames = driver.switchTo().frame(topframe).findElements(By.tagName("frame"));
System.out.println(nestedFrames.size());

On top you can see this page has nested frames inside the frame(frame_top).
Using line 1-3 I'm able to get the text of each nested frame. However I can't get number of frames inside the "Frame_top".(Line 4-5).
How do I get total number of frames inside the "frame_top"? Thanks in advance

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
zara
  • 99
  • 1
  • 8

2 Answers2

1

To get total number of nested frames with in the parent <frame> you need to:

  • Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
  • Induce WebDriverWait for the visibilityOfAllElementsLocatedBy() of the frames.
  • You can use either of the following Locator Strategies:
    • Using cssSelector and tagName:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("frame[name='frame-top']")));
      System.out.println(new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.tagName("frame"))).size());
      
    • Using xpath and tagName:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//frame[@name='frame-top']")));
      System.out.println(new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.tagName("frame"))).size());
      

Reference

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0
public static void main(String[] args) {
    // TODO Auto-generated method stub

    
    System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
    WebDriver d=new ChromeDriver();
    d.get("https://the-internet.herokuapp.com/");
    d.findElement(By.linkText("Nested Frames")).click();
    System.out.println("Pareent Frames Are "+d.findElements(By.tagName("frame")).size()+" in this Web Page");
    int parentFrames=d.findElements(By.tagName("frame")).size();
    d.switchTo().frame("frame-top");
    System.out.println("Child Frames are "+d.findElements(By.tagName("frame")).size()+" in the Web Page");
    int childFrames=d.findElements(By.tagName("frame")).size();
    d.switchTo().frame("frame-middle");
    System.out.println(d.findElement(By.id("content")).getText());
    System.out.println("Total Number of Frames are "+(parentFrames+childFrames));
    d.quit();
}
Rashid
  • 1