Zayonx
Zayonx

Reputation: 174

How to do an if this is not found then... Selenium node.js

This might be a dumb question but how can i do an if this is not found then... in my case ? I know that i have to use !== but i don't know how to handle it in my case... Here is the code :

 if (driver.findElement(By.xpath("//*[contains(@name, 'Discord')]"))) {
    fs.writeFile("NOTTHIS.txt", '${String(await driver.findElement(By.xpath("//*[contains(@name, 'Discord')]")))}', err => {
      if (err) throw err;
    })
    console.log("Aucun email detecté.")
    return;
  } else

Upvotes: 1

Views: 174

Answers (1)

Andrei
Andrei

Reputation: 5647

You can use

driver.findElements(By.xpath("//*[contains(@name, 'Discord')]"

it will give you a list with elements. And if the list length is 0, then there is no elements found. This way you won't get any Exception

if (driver.findElements(By.xpath("//*[contains(@name, 'Discord')]")).length !== 0) {
        fs.writeFile("NOTTHIS.txt", '${String(await driver.findElement(By.xpath("//*[contains(@name, 'Discord')]")))}', err => {
          if (err) throw err;
        })
        console.log("Aucun email detecté.")
        return;
      } else

Upvotes: 1

Related Questions