如何获取页面中的所有iframe [英] How to get all the iframes in a page

查看:1208
本文介绍了如何获取页面中的所有iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < html> 
< head>< / head>
< body>
< iframe> A< / iframe>
< table>
< div>
< span>
< div> < IFRAME> B< / iframe> < / DIV>
< style>
< div>
< div>




当我尝试在iframe中找到一个元素,它正在到达另一个iframe。我尝试使用driver.switchTo().frame(0)切换到iframe,但没有用。



当我尝试使用findElements方法获取所有iframe时,它仅返回一个iframe(B)。

解决方案

答案取决于你想要对iframe列表做什么。



如果你想要所有的列表,可以尝试使用递归函数。

  public void getAlliFrames(){
List< WebElement> iframes = driver.findElements(By.tagName(iframe)); (iframe.count()> 0){
driver.switchTo().frame(iframe.get(0));

if

getAlliFrames();
} else {
System.out.println(No more iframes);


PS:这是一个未经测试的代码,您的参考。


I have a very complex html page.

<html>
 <head></head>
  <body>
<iframe> A </iframe>
  <table>
   <div>
    <span>
      <div> <iframe> B </iframe> </div>
        <style>
          <div>
            <div>

When I try to locate an element in the iframe A it is getting to another iframe. I tried switching to the iframe using driver.switchTo().frame(0) but no use.

When I try to get all the iframes using the findElements method it is returning only one iframe (B).

解决方案

The answer depends on what you want to do with the list of iframes.

If you want all the list, you can try using recursive function.

public void getAlliFrames(){
   List<WebElement> iframes = driver.findElements(By.tagName("iframe"));

   if(iframe.count() > 0){
      driver.switchTo().frame(iframe.get(0));

      getAlliFrames();
   }else{
      System.out.println("No more iframes");
   }
}

PS: This is an untested code and just provided for your reference.

这篇关于如何获取页面中的所有iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆