如何在 Selenium 中處理框架?


藉助以下方法,我們可以在 Selenium 中處理框架 −

  • switchTo().frame( frameNumber)

    此方法將框架 id 用作引數。框架 id 的索引從 0 開始。如果沒有找到框架,將引發 NoSuchFrameException 異常。

  • switchTo().frame( frameName)

    此方法將開發者定義的框架名稱用作引數。框架名稱被視為字串,並用引號括起來。如果沒有找到框架,將引發 NoSuchFrameException 異常。

  • switchTo().frame( WebElement)

    此方法將 Web 元素用作引數。如果沒有找到框架,將引發 NoSuchFrameException 異常。如果框架不再處於活動狀態,將引發 StaleElementReferenceException 異常。

示例

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 FrameSet {
   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);
      //grabbing the frame with the help of frame name
      driver.switchTo().frame(“<<name expression>>“);
      //grabbing the frame with the help of frame webelement
      WebElement name = driver.findElement(By.name(“frame-right”));
      driver.switchTo().frame(driver.findElement(By.name(“<<name expression”)));
      driver.quit();
   }
}

更新日期: 2020 年 6 月 10 日

6000+ 次觀看

開始你的 職業生涯

完成課程獲得認證

開始吧
廣告
© . All rights reserved.