Reputation: 441
Any one can send me sample code how to verify element
in Selenium WebDrvier using Java
Upvotes: 42
Views: 344826
Reputation: 1347
webDriver.findElement(By.xpath("//*[@id='element']")).isDisplayed();
Upvotes: 1
Reputation: 613
To check if element is visible we need to use element.isDisplayed();
But if we need to check for presence of element anywhere in Dom we can use following method
public boolean isElementPresentCheckUsingJavaScriptExecutor(WebElement element) {
JavascriptExecutor jse=(JavascriptExecutor) driver;
try {
Object obj = jse.execute("return typeof(arguments[0]) != 'undefined' && arguments[0] != null;",
element);
if (obj.toString().contains("true")) {
System.out.println("isElementPresentCheckUsingJavaScriptExecutor: SUCCESS");
return true;
} else {
System.out.println("isElementPresentCheckUsingJavaScriptExecutor: FAIL");
}
} catch (NoSuchElementException e) {
System.out.println("isElementPresentCheckUsingJavaScriptExecutor: FAIL");
}
return false;
}
Upvotes: -1
Reputation: 528
Try using below code:
private enum ElementStatus{
VISIBLE,
NOTVISIBLE,
ENABLED,
NOTENABLED,
PRESENT,
NOTPRESENT
}
private ElementStatus isElementVisible(WebDriver driver, By by,ElementStatus getStatus){
try{
if(getStatus.equals(ElementStatus.ENABLED)){
if(driver.findElement(by).isEnabled())
return ElementStatus.ENABLED;
return ElementStatus.NOTENABLED;
}
if(getStatus.equals(ElementStatus.VISIBLE)){
if(driver.findElement(by).isDisplayed())
return ElementStatus.VISIBLE;
return ElementStatus.NOTVISIBLE;
}
return ElementStatus.PRESENT;
}catch(org.openqa.selenium.NoSuchElementException nse){
return ElementStatus.NOTPRESENT;
}
}
Upvotes: 5
Reputation: 37746
Here is my Java code for Selenium WebDriver. Write the following method and call it during assertion:
protected boolean isElementPresent(By by){
try{
driver.findElement(by);
return true;
}
catch(NoSuchElementException e){
return false;
}
}
Upvotes: 10
Reputation: 5082
I used java print statements for easy understanding.
To check Element Present:
if(driver.findElements(By.xpath("value")).size() != 0){
System.out.println("Element is Present");
}else{
System.out.println("Element is Absent");
}
Or
if(driver.findElement(By.xpath("value"))!= null){
System.out.println("Element is Present");
}else{
System.out.println("Element is Absent");
}
To check Visible:
if( driver.findElement(By.cssSelector("a > font")).isDisplayed()){
System.out.println("Element is Visible");
}else{
System.out.println("Element is InVisible");
}
To check Enable:
if( driver.findElement(By.cssSelector("a > font")).isEnabled()){
System.out.println("Element is Enable");
}else{
System.out.println("Element is Disabled");
}
To check text present
if(driver.getPageSource().contains("Text to check")){
System.out.println("Text is present");
}else{
System.out.println("Text is absent");
}
Upvotes: 79
Reputation: 306
To make sure that an element is present you can do the following:
driver.findElements(By.id("id"));
That will return an array, if that array size is > 0 then the element/s is present.
Also, you need to provide more information, such as language and what have you tried before asking,
Good luck
Upvotes: 0
Reputation: 1652
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.
Upvotes: 12