我们可以借助以下方法切换到Selenium中的帧-
switchTo()defaultContent()
此方法用于在帧和父帧之间来回切换。焦点转移到主页上。
switchTo()。parentFrame()
此方法用于将控件切换到当前帧的父帧。
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class FrameDefault {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "url with frames";
driver.get(url);
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
//在索引的帮助下抓取第一帧
driver.switchTo().frame(0);
System.out.println("Getting the page source " + driver.getPageSource());
//切换回父网页
driver.switchTo().defaultContent();
driver.quit();
}
}