Verifying whether an element present or visible in selenium Webdriver

+3 votes

How to verify element

  1. ispresent
  2. isvisible
  3. isenable
  4. textpresent
Apr 17, 2018 in Selenium by Vardy
• 2,360 points
97,085 views

6 answers to this question.

+3 votes
Best answer

Below code will help you:

  1. To check Element Present:

if(driver.findElement(By.xpath("value"))!= null){

System.out.println("Element is Present");

}else{

System.out.println("Element is Absent");

}

  1. To check Visible:

if( driver.findElement(By.cssSelector("a > font")).isDisplayed()){

System.out.println("Element is Visible");

}else{

System.out.println("Element is InVisible");

}

  1. To check Enable:
  2. if( driver.findElement(By.cssSelector("a > font")).isEnabled()){
  3. System.out.println("Element is Enable");
  4. }else{
  5. System.out.println("Element is Disabled");

}

  1. To check text present
  2. if(driver.getPageSource().contains("Text to check")){
  3. System.out.println("Text is present");
  4. }else{
  5. System.out.println("Text is absent");

}

For further understanding, you can refer to the Selenium Certification.

answered Apr 17, 2018 by king_kenny
• 3,710 points

selected Sep 25, 2018 by Priyaj
+3 votes
1. ispresent
answered Sep 25, 2018 by anonymous
Yup. As simple as it gets...
0 votes
To verify if element is present we use

1
ispresent
answered Sep 25, 2018 by Priyaj
• 58,020 points
ispresent() method is not available in selenium Library

may be deprecated or never really was part of the library
0 votes
Use below code

           bool isdisplay = driver.FindElement(By.Id("txt")).Displayed;
answered Jun 3, 2020 by Sri
• 3,190 points
0 votes

1. isDisplayed()

The isDisplayed method in Selenium verifies if a certain element is present and displayed. If the element is displayed, then the value returned is true. If not, then the value returned is false.

The code below verifies if an element with the id attribute value next is displayed.

Syntax:

boolean eleSelected= driver.findElement(By.xpath("xpath")).isDisplayed();

2. isSelected()

This method is often used on radio buttons, checkboxes or options in a menu. It is used to determine is an element is selected. If the specified element is selected, the value returned is true. If not, the value returned is false.

The code below verifies if an element with the id attribute value PersistentCookie is displayed.

Syntax:

boolean elePresent = driver.findElement(By.xpath("xpath")).isSelected();

3. isEnabled()

This method verifies if an element is enabled. If the element is enabled, it returns a true value. If not, it returns a false value.

The code below verifies if an element with the id attribute value next is enabled.

Syntax:

boolean eleEnabled= driver.findElement(By.xpath("xpath")).isEnabled();
answered Dec 14, 2020 by Gitika
• 65,770 points
0 votes

You could try something like:

    WebElement rxBtn = driver.findElement(By.className("icon-rx"));
    WebElement otcBtn = driver.findElement(By.className("icon-otc"));
    WebElement herbBtn = driver.findElement(By.className("icon-herb"));

    Assert.assertEquals(true, rxBtn.isDisplayed());
    Assert.assertEquals(true, otcBtn.isDisplayed());
    Assert.assertEquals(true, herbBtn.isDisplayed());

This is just an example. Basically, you declare and define the WebElement variables you wish to use and then Assert whether or not they are displayed. This is using TestNG Assertions.

answered Dec 14, 2020 by Rajiv
• 8,870 points

Related Questions In Selenium

0 votes
2 answers
0 votes
2 answers

Check that the element is clickable or not in Selenium WebDriver

Hi , you want to know only is ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
34,195 views
0 votes
1 answer

Verify that an element exist in Selenium 2 or not

If you are testing using JUnit and ...READ MORE

answered Aug 23, 2018 in Selenium by Meci Matt
• 9,460 points
5,711 views
0 votes
1 answer
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
13,672 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,142 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
7,220 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,872 views
0 votes
1 answer

Need some advise on the preferred element locators with Selenium WebDriver

Well let's not call it "Hierarchy". But, ...READ MORE

answered Apr 13, 2018 in Selenium by king_kenny
• 3,710 points
1,376 views
0 votes
1 answer

How to select an Object by its class in Selenium?

When it comes to Selenium, XPath will ...READ MORE

answered Apr 14, 2018 in Selenium by king_kenny
• 3,710 points
1,028 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP