
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Handle Authentication Popup with Selenium WebDriver in Java
We can handle authentication popup with Selenium. To do this, we have to pass the user credentials within the URL. We shall have 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
Let us work and accept the below authentication popup.
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(); } }
Output
Advertisements