【问题标题】:How to handle the errors in Selenium Webdriver Node Js. NoSuchElementError [duplicate]如何处理 Selenium Webdriver Node Js 中的错误。 NoSuchElementError [重复]
【发布时间】:2019-12-01 08:06:18
【问题描述】:

当我运行测试时,有时会出现以下错误:

"(node:34959) UnhandledPromiseRejectionWarning: NoSuchElementError: no such element: Unable to locate element:..."

然后测试停止。

我希望测试继续进行而不是停止,而是将消息写入控制台。

我现在的代码:

var one = await driver.findElement(By.xpath("//td[.='somethig']/following-sibling::td[1]/strong"));
var two = await one.getText();
await fs.appendFileSync("file.txt", two + ",");

这怎么可能?因为我不需要这个元素,为了进一步运行。 谢谢:)

【问题讨论】:

  • 这不是同一个问题人...:/

标签: node.js selenium selenium-webdriver error-handling


【解决方案1】:

您只需要将需要登录到控制台的错误包装在trycatch 中。

let one = '';
let two = '';
try {
    one = await driver.findElement(By.xpath("//td[.='somethig']/following-sibling::td[1]/strong"));
    two = await one.getText();
} catch (error) {
    console.log(error);
}
await fs.appendFileSync("file.txt", two + ",");  

【讨论】:

  • 谢谢。这几乎是好的。只是我不能接受一个功能,因为我总是得到“对象承诺”。 :/但这将是一个新问题。 :)
猜你喜欢
  • 2020-12-09
  • 2012-08-10
  • 2021-04-15
  • 2016-04-01
  • 2020-12-11
  • 2012-05-24
  • 2016-11-03
  • 2018-06-22
  • 1970-01-01
相关资源
最近更新 更多