- 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 do I verify that an element does not exist in Selenium 2?
We can verify if an element does not exist in Selenium webdriver. To achieve this, we shall use the method getPageSource which gets the entire page source. So we can obtain a complete page source and check if the text of the element exists.
We also use the findElements method and utilize any locators like xpath, CSS, and so on to identify the matching elements. The findElements gives an elements list.
We shall count the number of elements returned by the list with the help of the size method. If the value of size is greater than 0, then the element exists and if it is lesser than 0, then the element does not exist.
Let us verify if the highlighted text is present on the page −
Example
Code Implementation with findElements().
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import java.util.List; public class ExistElements{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("https://www.tutorialspoint.com/index.htm"); String str = "You are browsing the best resource for Online Education"; // identify elements List<WebElement> m= driver.findElements(By.xpath("//*[contains(text(),'You are browsin')]")); // verify size if ( m.size() > 0){ System.out.println("Text: " + str + " is present. "); } else{ System.out.println("Text: " + str+ " is not present. "); } driver.quit(); } }
Example
Code Implementation with getPageSource.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class ElementExistPgSource{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("https://www.tutorialspoint.com/index.htm"); String str = "You are browsing the best resource for Online Education"; //get page source if ( driver.getPageSource().contains("You are browsin")){ System.out.println("Text: " + str + " is present. "); } else{ System.out.println("Text: " + str + " is not present. "); } driver.quit(); } }
Output
- Related Articles
- How do I find an element that contains specific text in Selenium Webdriver?
- Java Selenium Chromedriver.exe Does not Exist IllegalStateException
- How do I find an element that contains specific text in Selenium WebDriver (Python)?
- How to verify an attribute is present in an element using Selenium WebDriver?
- How to verify if an element is displayed on screen in Selenium?
- How can I create a python directory if it does not exist?
- How can I check if some text exist or not in the page using Selenium?
- How do I select data that does not have a null record in MySQL?
- Does NOT EQUAL exist in MySQL?
- How can I create a directory if it does not exist using Python?
- How to insert only those records that does not exist in a MySQL table?
- How to select from MySQL table A that does not exist in table B?
- How can I delete an element in Selenium using Python?
- How to verify color of a web element in Selenium Webdriver?
- How do I get current URL in Selenium Webdriver 2 Python?
