Webdriver定位之iframe中的元素定位
-
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"))
----夫英雄者,胸怀大志,腹有良谋,有包藏宇宙之机,吞吐天地之志者也。