sininen
sininen

Reputation: 563

Wait for an element to be present or return false

It is possible to wait until an element is present on a page with browser.wait(EC.presenceOf(element(by.id("button"))), 5000, "button not found")

This will throw an error when the element is not found on the page. I do not want this function to throw an error, instead it should return false if the element is not found after 5 seconds.

How can I achieve this?

Upvotes: 1

Views: 544

Answers (2)

Ketan More
Ketan More

Reputation: 36

you can try using if else block around with isdisplayed() function like

if(driver.findElement(By.xpath(noRecordId)).isDisplayed()){
return true;
}
else{
return false;
}

you can use boolean variable in place of return to save result.

Upvotes: 0

DublinDev
DublinDev

Reputation: 2348

One approach you can attempt to achieve this is by using standard try/catch's. This will catch the exception that is produced from the browser.wait and handle it in the catch.

try{
   browser.wait(EC.presenceOf(element(by.id("button"))), 5000, "button not found")
}catch(){
   return false;
};

Upvotes: 3

Related Questions