gpt4 book ai didi

node.js - 如何处理 Selenium Webdriver Node Js 中的错误。 NoSuchElementError

转载 作者:行者123 更新时间:2023-12-03 08:20:57 38 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





NoSuchElementException, Selenium unable to locate element

(3 个回答)


2年前关闭。




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

“( Node :34959)UnhandledPromiseRejectionWarning:NoSuchElementError:没有这样的元素:无法找到元素:...”

并且测试停止。

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

我现在的代码:

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 + ",");

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

最佳答案

您只需要将需要登录到控制台的错误包装在try 中即可和 catch .

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 + ",");

关于node.js - 如何处理 Selenium Webdriver Node Js 中的错误。 NoSuchElementError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57161406/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com