使用 Java 透過 Selenium WebDriver 處理認證彈出視窗


我們可以使用 Selenium 處理身份驗證彈出視窗。為此,我們必須在 URL 中傳遞使用者憑證。我們必須將使用者名稱和密碼新增到 URL。

語法

https://username:password@URL
https://admin:admin@the−nternet.herokuapp.com/basic_auth
Here, the admin is the username and password.
URL − www.the-internet.herokuapp.com/basic_auth

讓我們接受並處理下面的身份驗證彈出視窗。

示例

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class AuthnPopup{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
      "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String u = "admin";
      // adding username, password with URL
      String str = "https://" + u + ":" + u + "@" +
      "the-internet.herokuapp.com/basic_auth";
      driver.get(str);
      // identify and get text after authentication of popup
      String t = driver.findElement(By.cssSelector("p")).getText();
      System.out.println("Text is: " + t);
      driver.quit();
   }
}

輸出

更新於: 2020 年 11 月 30 日

4K+ 瀏覽

開始你的職業生涯

完成課程後獲得認證

開始
廣告
© . All rights reserved.