0 votes
1 view
in Devops and Agile by (19.8k 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 (63.4k 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! 

 

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...