- 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
Verifying whether an element present or visible in Selenium Webdriver
We can verify whether an element is present or visible in a page with Selenium webdriver. To check the presence of an element, we can use the method – findElements.
The method findElements returns a list of matching elements. Then, we have to use the method size to get the number of items in the list. If the size is 0, it means that this element is absent from the page.
Syntax
int j = driver.findElements(By.id("txt")).size();
To check the visibility of an element in a page, the method isDisplayed is used. It returns a Boolean value( true is returned if the element is visible, and otherwise false).
Syntax
boolean t = driver.findElement(By.name("txt-val")).isDisplayed();
Example
Code Implementation for element visible.
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 ElementVisible{ 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.tutorialspoint.com/about/about_careers.htm"); // identify element with partial link text WebElement n =driver.findElement(By.partialLinkText("Refund")); //check if element visible boolean t = driver.findElement(By.partialLinkText("Refund")).isDisplayed(); if (t) { System.out.println("Element is dispalyed"); } else { System.out.println("Element is not dispalyed"); } driver.quit(); } }
Output
Example
Code Implementation for element presence.
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 ElementPresence{ 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.tutorialspoint.com/about/about_careers.htm"); //check if element present int t = driver.findElements(By.partialLinkText("Refund")).size(); if (t > 0) { System.out.println("Element is present"); }else { System.out.println("Element is not present"); } driver.quit(); } }
Output
- Related Articles
- How to create a Javascript executor for making an element visible in Selenium Webdriver?
- How to verify an attribute is present in an element using Selenium WebDriver?
- Test if element is present using Selenium WebDriver?
- How to check if an element is visible with WebDriver?
- Finding an element in a sub-element in Selenium Webdriver.
- How to set whether an element should be visible in JavaScript?
- Check that the element is clickable or not in Selenium WebDriver
- Set whether or not an element should be visible while not facing the screen with JavaScript?
- Test if an element is focused using Selenium Webdriver.
- How to get an attribute value of an element in Selenium Webdriver?
- How to wait until an element is present in Selenium?
- How do I find an element that contains specific text in Selenium Webdriver?
- Determine whether an element should be visible when not facing the screen with CSS
- How to click on hidden element in Selenium WebDriver?
- Send keys without specifying element in Python Selenium webdriver

Advertisements