如何在 Selenium 中切換到框架?


我們可以用以下方法在 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);
      //grabbing the first frame with the help of index
      driver.switchTo().frame(0);
      System.out.println("Getting the page source " + driver.getPageSource());
      // switching back to the parent web page
      driver.switchTo().defaultContent();
      driver.quit();
   }
}

更新於: 2020 年 6 月 10 日

5K+ 檢視數

開啟你的事業

完成課程以獲得認證

馬上開始
廣告