1 import java.io.File; 2 3 import org.openqa.selenium.By; 4 import org.openqa.selenium.WebDriver; 5 import org.openqa.selenium.WebElement; 6 import org.openqa.selenium.chrome.ChromeDriver; 7 8 /* 9 * iframe 处理 10 */ 11 public class FrameTest { 12 13 public static void main(String[] args) { 14 WebDriver dr = new ChromeDriver(); 15 16 File file = new File("learning/main.html"); 17 String filePath = "file:///" + file.getAbsolutePath(); 18 System.out.println("now accesss " + filePath); 19 20 dr.get(filePath); 21 22 String defaultID1 = dr.findElement(By.id("id1")).getText(); 23 System.out.println("default ID1 text: " + defaultID1); 24 25 // 进入 id = "frame" 的 frame 中, 定位 id = "div1"; div = "input1" 26 dr.switchTo().frame("frame"); 27 String frameDiv1 = dr.findElement(By.id("div1")).getText(); 28 System.out.println("frame Div1 text: " + frameDiv1); 29 30 WebElement element = dr.findElement(By.id("input1")); 31 System.out.println("frame Input1 tagName: " + element.getTagName()); 32 33 // 返回默认的 frame 34 dr.switchTo().defaultContent(); 35 String defaultID2 = dr.findElement(By.id("id1")).getText(); 36 System.out.println("default ID1 text: " + defaultID2); 37 38 dr.quit(); 39 } 40 41 }
原文:http://www.cnblogs.com/Roger1227/p/3833686.html