Iframe là việc nhúng một html vào trang web hiện tại. Có 2 cách là nhúng 1 trang tĩnh và nhúng thông qua remote.
Selenium chỉ hiểu html mặc định mà không thể tương tác với 2 html trong cùng một thời điêm. Nên để làm việc được chúng ta phải switchTo()
–> frame()
Test case
@Test
void frame() {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://the-internet.herokuapp.com/nested_frames");
driver.switchTo().frame("frame-top");
driver.switchTo().frame("frame-left");
System.out.println(driver.findElement(By.xpath("html/body")).getText());
driver.switchTo().parentFrame();
driver.switchTo().frame("frame-middle");
System.out.println(driver.findElement(By.id("content")).getText());
driver.switchTo().parentFrame();
driver.switchTo().frame("frame-right");
System.out.println(driver.findElement(By.xpath("html/body")).getText());
driver.switchTo().parentFrame();
driver.switchTo().parentFrame();
driver.switchTo().frame("frame-bottom");
System.out.println(driver.findElement(By.xpath("html/body")).getText());
}
More …
- Ví dụ ở trên chúng ta thấy các frame lồng nhau hay gọi là
nested frame
cho nên chúng ta sẽ dùngdriver.switchTo().frame()
để có thể đi tới frame chúng ta cần làm việc. - Và để có thể trở lại frame cha selenium cũng cho ta
driver.switchTo().parentFrame()
- Test case ở trên sẽ là đi từ trang gốc –> vào
frame-top
–>frame-left
–> trở lạiframe-top
–>frame-middle
–> trở lạiframe-top
–>frame-right
–> trở lạiframe-top
–> trỏ lại trang gốc –> nhảy vàoframe-bottom