-
main.html
- <</span>html>
-
<</span>head> -
<</span>title>FrameTest</</span>title> -
</</span>head> -
<</span>body> -
<</span>div id = "id1">thisis </</span>div>a div! -
<</span>iframe id = "frame1"scrolling="no" style="left:0;position:absolute;" src = "frame.html"></</span>iframe> -
</</span>body> - </</span>html>
-
-
- frame.html
- <</span>html>
-
<</span>head> -
<</span>title>this is </</span>title>a frame! -
</</span>head> -
<</span>body> -
<</span>div id = "div1">thisis </</span>div>a div,too! -
<</span>label>input:</</span>label> -
<</span>input id = "input1"></</span>input> -
</</span>body> - </</span>html>
1.iFrame有ID 或者 name的情况
//进入id="frame1"的frame中,定位id="div1"的div和id="input1"的输入框。
dr.switchTo().frame("frame1");
dr.findElement(By.id("div1"));
dr.findElement(By.id("input1"))
2.如果一个iFrame既没有id,也没有name,通用情况
// 定位frame位置,并选取frame
WebElement frame=driver.findElement(By.xpath( "/html/body/div[2]/div[8]/div[2]/div[3]/div/div[2]/div/iframe" ));
driver.switchTo().frame(frame);
3.跳出iFrame
//跳出frame,进入default content;重新定位id="id1"的div
dr.switchTo().defaultContent();
dr.findElement(By.id("id1"))