1

I want to console.log("No element") when there is no button element avalible but it does not work.

const {Builder, By} = require("selenium-webdriver");

let driver = new Builder().forBrowser("chrome").build();

async function myfun(){

    await driver.get("website")
    await driver.findElement(By.linkText("yes")).click();
    await driver.findElement(By.linkText("yes")).click();
    await driver.sleep(500);
    await driver.findElement(By.id("inputfield")).sendKeys("name");

    setInterval(function(err){
        if (err){
            console.log("No element");
        }else{
            driver.findElement(By.tagName("button")).click();
        }
    }, 1000);

}
myfun()

I get these errors:

(node:16248) UnhandledPromiseRejectionWarning: NoSuchElementError: no such element: Unable to locate element: {"method":"css selector","selector":"button"}
  (Session info: chrome=81.0.4044.92)
(node:16248) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:16248) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I have also tried the try/cath method but with no luck.

try {
    driver.findElement(By.tagName("button")).click();
} catch(err) {
    console.log("No element")
}
Abdalla Roda
  • 607
  • 1
  • 6
  • 13
  • 1
    Check this: https://stackoverflow.com/questions/20148857/check-if-element-exists-selenium-javascript-node-js – Ben Apr 17 '20 at 10:00

1 Answers1

0

Credit to user Arthur Weborg. I found this post which solved my problem perfectly. It is copied from another thread. For a clearer understanding see the full thread here.

The selected answer did not work as expected (err.state was undefined and a NoSuchElementError was always thrown) -- though the concept of using the optional callbacks still works.

Since I was getting the same error as the OP is referencing, I believe NoSuchElementError should be referenced when determining if the targeted element exists or not. As it's name implies is the error that is thrown when an element does not exist. So the condition in the errorCallback should be:

err instanceof webdriver.error.NoSuchElementError

So the complete code block would be as follows (I also am using async/await for those taking advantage of that syntax):

var existed = await driver.findElement(webdriver.By.id('test')).then(function() {
    return true;//it existed
}, function(err) {
    if (err instanceof webdriver.error.NoSuchElementError) {
        return false;//it was not found
    } else {
        webdriver.promise.rejected(err);
    }
});
//handle value of existed appropriately here

Abdalla Roda
  • 607
  • 1
  • 6
  • 13