- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to capture the text from Alert Message in Selenium Webdriver?
We can capture the text from the alert message in Selenium webdriverwith the help of the Alert interface. By default, the webdriver object has control over the main page, once an alert pop-up gets generated, we have to shift the webdriver focus from the main page to the alert.
This is done with the help of the switchTo().alert() method. Once the driver focus is shifted, we can obtain the text of the pop-up with the help of the method switchTo().alert().getText(). Finally we shall use the accept method to accept the alert and dismiss method to dismiss it.
Let us take an example of the below alert and obtain its message −
Syntax
Alert a = driver.switchTo().alert(); String s= driver.switchTo().alert().getText(); a.accept();
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import java.util.concurrent.TimeUnit; public class JsEnterText{ public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); //implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //URL launch driver.get("https://the-internet.herokuapp.com/javascript_alerts"); // identify element WebElement l = driver. findElement(By.xpath("//*[text()='Click for JS Alert']")); l.click(); //switch focus to alert Alert a = driver.switchTo().alert(); //get alert text String s= driver.switchTo().alert().getText(); System.out.println("Alert text is: " + s); //accepting alert a.accept(); driver.quit(); } }
Output
- Related Articles
- How to extract text from a Javascript alert in Selenium with python?
- How to get Tooltip Text in Selenium Webdriver?
- How I can change alert message text color using JavaScript?
- How to get typed text from a textbox by using Selenium Webdriver?
- How to center the JavaScript alert message box?
- How to capture screenshots in Selenium?
- How to capture the screenshot of a specific element rather than entire page using Selenium Webdriver?
- How to capture and print Python exception message?
- How can I verify Error Message on a webpage using Selenium Webdriver?
- How to capture tooltip in Selenium using getAttribute?
- How to Select Value from DropDown using Selenium Webdriver?
- How to capture tooltip in Selenium using Actions Class?
- How to navigate back to current page from Frame in selenium webdriver?
- How to switch back from a frame to default in Selenium Webdriver?
- How to check if an alert exists using WebDriver?

Advertisements