Selenium和Node.JS ElementNotFoundError或NoSuchElementError

时间:2015-11-10 10:55:08

标签: javascript selenium

我试图找到一种方法来截取屏幕截图(我已经创建了方法),任何时候都会出现“ElementNotFoundError”错误消息。'无需在脚本中的任何地方定义它(例如,每次我通过" .then(driver.findElement(By.id())搜索元素。点击())",我不&# 39; t必须始终指定在' catch'。

中截取屏幕截图

这是一个典型的ElementNotFound错误:

/Users/nhyland/Documents/www/Selenium/node_modules/selenium-webdriver/lib/goog/async/nexttick.js:41
goog.global.setTimeout(function() { throw exception; }, 0);
^
NoSuchElementError: Unable to locate element: {"method":"class name","selector":"loginID"}
at new bot.Error (/Users/nhyland/Documents/www/Selenium/node_modules/selenium-webdriver/lib/atoms/error.js:108:18)
at Object.bot.response.checkResponse (/Users/nhyland/Documents/www/Selenium/node_modules/selenium-webdriver/lib/atoms/response.js:109:9)
at /Users/nhyland/Documents/www/Selenium/node_modules/selenium-webdriver/lib/webdriver/webdriver.js:379:20
at promise.Promise.goog.defineClass.invokeCallback_ (/Users/nhyland/Documents/www/Selenium/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:1337:14)

1 个答案:

答案 0 :(得分:0)

找到答案..这是我创建的代码:

webdriver.promise.controlFlow().on('uncaughtException', e => {
    console.error('Unhandled error. Taking screenshot.');
    console.log(util.inspect(e.message));
    if(e.name) {
        TakeScreenshot(driver, './Screenshots/'+ e.name);
    } else {
        TakeScreenshot(driver, './Screenshots/UncaughtNoState');
    }
});