Computer >> Máy Tính >  >> Lập trình >> Java

Làm cách nào chúng ta có thể xử lý cửa sổ bật lên xác thực trong Selenium WebDriver bằng Java?

Chúng tôi có thể xử lý cửa sổ bật lên xác thực trong Selenium webdriver bằng cách sử dụng Java. Để làm điều này, chúng tôi phải chuyển thông tin đăng nhập của người dùng trong URL. Chúng tôi sẽ phải thêm tên người dùng và mật khẩu vào URL.

Cú pháp -

https://username:password@URL
https://admin:admin@the-internet.herokuapp.com/basic_auth
Here, the admin is the username and password.
URL – www.the-internet.herokuapp.com/basic_auth
Let us work and accept the below authentication popup.

Làm cách nào chúng ta có thể xử lý cửa sổ bật lên xác thực trong Selenium WebDriver bằng Java?

Ví dụ

Triển khai mã.

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);

      //close browser
      driver.close();
   }
}

Đầu ra

Làm cách nào chúng ta có thể xử lý cửa sổ bật lên xác thực trong Selenium WebDriver bằng Java?