package com.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Test_frame {
public static void main(String[] args) {
String url = "file:///C:/Documents and Settings/fei yong/桌面/main.html";
//打开chrome
WebDriver dr = new ChromeDriver();
dr.get(url);
//在default content定位id="id1"的div
dr.findElement(By.id("id1"));
//此时,没有进入到id="frame"的frame中时,以下两句会报错
//dr.findElement(By.id("div1"));//报错
//dr.findElement(By.id("input1"));//报错
//进入id="frame"的frame中,定位id="div1"的div和id="input1"的输入框。
dr.switchTo().frame("frame");
dr.findElement(By.id("div2"));
dr.findElement(By.id("input2"));
System.out.println("div2.getTagName:"+dr.findElement(By.id("div2")).getTagName()+";");
System.out.println("input2.getTagName:"+dr.findElement(By.id("input2")).getTagName());
System.out.println("input2.getText:"+dr.findElement(By.id("input2")).getText());
//此时,没有跳出frame,如果定位default content中的元素也会报错。
//dr.findElement(By.id("id1"));//报错
//跳出frame,进入default content;重新定位id="id1"的div
dr.switchTo().defaultContent();
dr.findElement(By.id("id1"));
System.out.println("id1.getText:"+dr.findElement(By.id("id1")).getText());
dr.quit();
}
}