How to get all the iframes in a page

随声附和 提交于 2020-01-07 02:45:10

问题


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).


回答1:


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.



来源:https://stackoverflow.com/questions/24915850/how-to-get-all-the-iframes-in-a-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!