Selenium 如何获取框架内嵌套框架的总数
带有3个嵌套框架的框架顶部快照: 代码:Selenium 如何获取框架内嵌套框架的总数,selenium,nested,webdriverwait,frames,expected-condition,Selenium,Nested,Webdriverwait,Frames,Expected Condition,带有3个嵌套框架的框架顶部快照: 代码: 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); Li
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 nestedFrames=driver.switchTo().frame(topframe).findElements(按.tagName(“frame”));
System.out.println(nestedFrames.size());
在顶部,您可以看到此页面的框架内有嵌套的框架(框架顶部)。使用第1-3行,我能够得到每个嵌套帧的文本。但是,我无法获取“Frame_top”中的帧数(第4-5行)。
如何获取“frame_top”中的总帧数?
提前感谢要获取父级中的嵌套帧总数,您需要:
- 诱导WebDriverWait使所需的框架可用并切换到它
- 诱导WebDriverWait,以获得由帧的()表示的所有元素的可见性
- 您可以使用以下任一选项:
- 使用CSS选择器和标记名:
- 使用xpath和标记名:
参考文献 您可以在以下内容中找到一些相关讨论:
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());
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());
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());