- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 can I verify Error Message on a webpage using Selenium Webdriver?
We can verify error messages on a webpage using Selenium webdriver using the Assertion. In case, the actual and expected values do not match, an Assertion Error is thrown.
Let us try to verify the highlighted error message.
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; import org.testng.Assert; public class VerifyErrorMsg{ 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://www.linkedin.com/"); // identify element WebElement l = driver.findElement(By.id("session_key")); l.sendKeys("abc"); WebElement t = driver.findElement(By.className("sign-in-form__submit-button")); t.click(); //expected error text String exp = "Please enter a valid email address or mobile number."; //identify actual error message WebElement m = driver.findElement(By.className("alert-content")); String act = m.getText(); System.out.println("Error message is: "+ act); //verify error message with Assertion Assert.assertEquals(exp, act); driver.quit(); } }
Output
- Related Articles
- How to refresh a webpage using Python Selenium Webdriver?
- How to Verify Tooltip using Selenium WebDriver?
- How to send keyboard input to a textbox on a webpage using Python Selenium webdriver?
- How can I handle multiple keyboard keys using Selenium Webdriver?
- How to handle SSL certificate error using Selenium WebDriver?
- Is it possible to scroll down in a webpage using Selenium Webdriver programmed on Python?
- How can I scroll a web page using selenium webdriver in python?
- How can I close a specific window using Selenium WebDriver with Java?
- How can I select date from a datepicker div using Selenium Webdriver?
- How can I clear text of a textbox using Python Selenium WebDriver?
- How can I get Webdriver Session ID in Selenium?
- How do I print a message to the error console using JavaScript?
- How to loop through a menu list on a webpage using Selenium?
- How to verify an attribute is present in an element using Selenium WebDriver?
- How to verify color of a web element in Selenium Webdriver?

Advertisements