Any one can send me sample code how to verify element
in Se
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;
}
}
webDriver.findElement(By.xpath("//*[@id='element']")).isDisplayed();
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");
}
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;
}
}
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
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;
}