如何获取框架内嵌套框架的总数 [英] How to get total number of nested frames inside the frame
问题描述
具有 3 个嵌套框架的 Frame_top 快照:
Snapshot of Frame_top with 3 nested frames:
代码:
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());
在上面你可以看到这个页面在frame(frame_top)里面有嵌套的frame.
使用第 1-3 行,我可以获得每个嵌套框架的文本.但是,我无法获得Frame_top"内的帧数.(第 4-5 行).
如何获得frame_top"内的总帧数?提前致谢
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
推荐答案
要获取父 中嵌套 frames 的总数,您需要:
To get total number of nested frames with in the parent <frame>
you need to:
- 为所需的 frameToBeAvailableAndSwitchToIt 引入 WebDriverWait.
- 为 frames 的 visibilityOfAllElementsLocatedBy() 引入 WebDriverWait.
- 您可以使用以下任一定位器策略:
使用 cssSelector 和 tagName:
- 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());
使用 xpath 和 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());
- iframe下#document的处理方式
- Selenium Webdriver Java 中是否可以在不使用 driver.switchTo().frame(frameName") 的情况下切换到框架中的元素?
您可以在以下位置找到一些相关讨论:
You can find a couple of relevant discussions in:
这篇关于如何获取框架内嵌套框架的总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!