如何使用 Selenium webdriver 處理模態對話方塊?


我們可以使用 Selenium 處理模態對話方塊。模態就像強制使用者在返回實際頁面之前對其進行訪問的視窗。它也可以是一個身份驗證視窗。

讓我們使用以下模態對話方塊 −

示例

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ModDialog{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("http://www.uitestpractice.com/Students/Switchto");
      // identify element and click
      WebElement m = driver
      .findElement(By.xpath("//button[contains(text(), 'Launch modal')]"));
      // identify modal header and obtain text
      WebElement m=
      driver.findElement(By.xpath("//h4[@class='modal−title']"));
      System.out.println("Modal Dialog text: " + m.getText());
      // click on OK
      WebElement n= driver.findElement(By.xpath("//button[text()='Ok']"));
      n.click();
      driver.quit();
   }
}

輸出

更新於: 28-11-2020

4K+ 次瀏覽

啟動您的 職業

透過完成課程獲得認證

開始
廣告