Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

With WebDriver from Selenium 2.0a2 I am having trouble checking if an element is visible.

WebDriver.findElement returns a WebElement, which unfortunately doesn't offer an isVisible method. I can go around this by using  WebElement.clear  or  WebElement.click both of which throw an ElementNotVisibleException, but this feels very dirty.

Any better ideas?

1 Answer

0 votes
by (62.9k points)

  The 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"); 

}

2. To check Visible:

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

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

}

else{

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

}

3. To check Enable:

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

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

}

else

{

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

}

4. To check text present

 if(driver.getPageSource().contains("Text to check")){

System.out.println("Text is present");

}

else

{

System.out.println("Text is absent");

}

Hope this helps! 

 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...