Check if element exists - selenium / javascript / node-js

后端 未结 6 1445
花落未央
花落未央 2020-12-14 12:21

I\'m trying to check if an element exists before I can execute this line:

driver.findElement(webdriver.By.id(\'test\'));

This throws an error \"

6条回答
  •  情书的邮戳
    2020-12-14 13:15

    You want to check whether an element exists before you want to find an element on UI. You can wait until the element gets located on UI then you can perform the find element operation.

    Ex: Below code waits until element located then it will perform the find element.

    driver.wait(webdriver.until.elementLocated(webdriver.By.id(LocatorValue)),20000)
    .then(()=>{
       return driver.findElement(webdriver.By.id('test'));
    }).then((element)=>{
        // Perform any operation what you to do.
          return element.click();
    }).catch(()=>{
         console.log('Element not found');
    })
    

提交回复
热议问题