[英]Unhandled Promise Rejection Warning - Node.js
I'm trying to make JS load a website and then click on two buttons. 我正在尝试使JS加载一个网站,然后单击两个按钮。 The first button clicks and passes, but the second one shoots out this error
第一个按钮单击并通过,但是第二个按钮发出该错误
const ATC_Button = driver.wait(
webdriver.until.elementLocated({ name: 'commit' }),
20000
);
const GTC_Button = driver.wait(
webdriver.until.elementLocated({ xpath: '//*[@id="cart"]/a[2]' }),
20000
);
ATC_Button.click();
GTC_Button.click();
Error: 错误:
(node:21408) UnhandledPromiseRejectionWarning: WebDriverError: element not visible
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 10.0.16299 x86_64)
at Object.checkLegacyResponse (C:\New folder\JS\Bot V1\MYBot\node_modules\selenium-webdriver\lib\error.js:585:15)
at parseHttpResponse (C:\New folder\JS\Bot V1\MYBot\node_modules\selenium-webdriver\lib\http.js:533:13)
at Executor.execute (C:\New folder\JS\Bot V1\MYBot\node_modules\selenium-webdriver\lib\http.js:468:26)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:21408) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function
without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:21408) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
I'm not sure how to handle errors in JS, I'm still learning. 我不确定如何处理JS中的错误,我仍在学习。 Can someone explain?
有人可以解释吗?
In selenium driver.wait
returns an IThenable
or Promise
. 在selenium
driver.wait
返回IThenable
或Promise
。 Promises are just a way to do asynchronous programming in javascript, and they have two functions, then
and `catch, the latest is how you would handle rejections (errors) inside promises. 承诺只是用javascript进行异步编程的一种方法,它们有两个函数,
then
`catch',最新的是您如何处理承诺中的拒绝 (错误)。 So, your code needs to be something like: 因此,您的代码必须类似于:
const ATC_Button = driver.wait(
webdriver.until.elementLocated({ name: 'commit' }),
20000
).catch(function(err){
// Do something with you error
});
const GTC_Button = driver.wait(
webdriver.until.elementLocated({ xpath: '//*[@id="cart"]/a[2]' }),
20000
).catch(function(err){
// Do something with you error
});
For further reference I find this article to be a nice introduction to promises. 为了进一步参考,我觉得这个文章是一个很好的介绍,以承诺。
Update 更新
Your issue is most likely because you are trying to click
the button before is located, so, since your driver.wait
returns a WebElementPromise
(a promise of a WebElement) there are two options: 您的问题最有可能是因为您试图
click
位于按钮之前,因此,由于driver.wait
返回WebElementPromise
(对WebElementPromise
的承诺),因此有两个选项:
driver
.wait(webdriver.until.elementLocated({ name: "commit" }), 20000)
.then(button => button.click()) // Here the WebElement has been returned
.catch(function(err) {
// Do something with you error
});
Note: This is only available for ES6 注意:这仅适用于ES6
// Here you are waiting for the promise to return a value
const ATC_Button = await driver
.wait(webdriver.until.elementLocated({ name: "commit" }), 20000)
.catch(function(err) {
// Do something with you error
});
ATC_Button.click();
ps: Since you say you are still learning I assume there are terms here that might not know if that is true I recommend you to do research. ps:因为您说您还在学习,所以我认为这里有些术语可能不知道这是否成立,因此建议您进行研究。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.