Selenium多层级的iframe中元素的定位

很多时候我们遇到多层级的iframe就会想各种方法去获取iframe中的元素,但其实很简单就可以做到的,就是一级一级获取就可以了,获取至你需要的那个层级即可,下面看下实际的案例;(转)

<frame src="" id="index_main" name="main" scrolling="Yes" noresize="noresize">
        <iframe id="Editor1" src="" frameborder="0" scrolling="no" >
              <iframe id="eWebEditor" width="100%" height="100%" scrolling="yes" frameborder="0" src="">
                  <input type="text" id="TeacherTxt" name="Teacher" size="12" maxlength="12" >
            </iframe>
        </iframe>
</iframe>
 
示例:
         @Test  
    public void phoneLogin() throws Exception{  

                WebDriver chrome = new ChromeDriver();
         chrome.switchTo().frame("index_main");
       chrome.switchTo().frame("Editor1");
       chrome.switchTo().frame("eWebEditor");
       chrome.findElement(By.id(" TeacherTxt")).sendKeys("测试Iframe");

       chrome.switchTo().defaultContent();  
            }
 
注意:最后只需要退出一次iframe即可;
原文地址:https://www.cnblogs.com/weiwei-python/p/9808004.html