luckyvasul
luckyvasul

Reputation: 58

Cannot check the WebElement is Present or Not by "isDisplyed" on selenium WebDriver

I am trying to get the value from UI, using (By.id) locator.getAtrribute("value"),

Issue:

But, when the xpath is not found (web element not present on UI), I need to take the value as '00:00', but I get error as "No Such element"

I need to add a check, to find the webelement is present on UI, if true, get value using getattribute("value") , else return the value as '00:00'

But When using If condition, "(WebelementforUIvalue.isEmpty()" returns '00:00', though the Web element is present on UI. (I need to take 00:00, when there is no element found/present on UI)

String WebelementforUIvalue = driver.findElement(By.id("ctl00_MainContent_AllowanceGridView_SRow"+cellRosterPerioddd+cellRosterPeriodmm+"_"+PayBucket)).getAttribute("value");
if (WebelementforUIvalue.isEmpty()) {
    UIValue = "00.00";  
} else {
    UIValue = WebelementforUIvalue;   
} 
System.out.println(UIValue);

Upvotes: 0

Views: 543

Answers (4)

Mark Rowlands
Mark Rowlands

Reputation: 5453

Personally I'd use a try/catch block for this particular case.

try {
    String WebelementforUIvalue = driver.findElement(By.id("ctl00_MainContent_AllowanceGridView_SRow"+cellRosterPerioddd+cellRosterPeriodmm+"_"+PayBucket)).getAttribute("value");
    UIValue = WebelementforUIvalue;
    } 
    catch (NoSuchElementException) {
        UIValue ="00.00";
    }

Upvotes: 2

Subh
Subh

Reputation: 4424

Try like this instead(Java Code). :

    try{
       WebElement element = new WebDriverWait(driver,20).until(ExpectedConditions.presenceOfElementLocated(By.id("ctl00_MainContent_AllowanceGridView_SRow"+cellRosterPerioddd+cellRosterPeriodmm+"_"+PayBucket));
       UIValue = element.getAttribute("value");
    }catch(Throwable e){
       UIValue = "00.00";
    }
System.out.println(UIValue);

I have given an explicit timeout of 20 seconds. So, selenium will try to detect the presence of element in 20 seconds. If it doesn't find then it will timeout and send the assign "UIValue" to "00.00" or else if it finds the element it will assign the "value" attribute's content to "UIValue".

Upvotes: 1

Uday
Uday

Reputation: 1484

You can handle this in better way using "findElements"(observer 's' at the end). findElements returns the list of elements matching the locating criteria.

List<WebElements> list=driver.findElements(By.id("Your criteria"))
Integer intNoValues=list.size();
if(intNoValues=0)
{  
  UIValue = "00.00";
}
else
{
 UIValue = WebelementforUIvalue;
}

If you want a detailed explanation of findElements, you can watch the below video How to find element which doesnt exist using WebDriver

Upvotes: 0

Vivek Singh
Vivek Singh

Reputation: 3649

you can try it out this way...

String WebelementforUIvalue = null;
try{
    WebelementforUIvalue = driver.findElement(By.id("ctl00_MainContent_AllowanceGridView_SRow"+cellRosterPerioddd+cellRosterPeriodmm+"_"+PayBucket)).getAttribute("value");
   } catch(NoSuchElementException nsee) {
           WebelementforUIvalue = "0.0";
   }
System.out.println(WebelementforUIvalue);

Upvotes: 0

Related Questions