Home > Java > javaTutorial > How to handle authentication pop-ups using Java and Selenium WebDriver?

How to handle authentication pop-ups using Java and Selenium WebDriver?

WBOY
Release: 2023-08-18 21:41:05
forward
937 people have browsed it

We can use Selenium to handle authentication pop-ups. In order to do this, we have to pass user credentials in the URL. We need to add the username and password to the URL.

Syntax

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
Copy after login

Let’s work and accept the authentication popup below.

如何使用Java和Selenium WebDriver处理身份验证弹窗?

Example

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();
   }
}
Copy after login

Output

如何使用Java和Selenium WebDriver处理身份验证弹窗?

The above is the detailed content of How to handle authentication pop-ups using Java and Selenium WebDriver?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template