[英]Unhandled Promise Rejection Warning - Node.js
我正在嘗試使JS加載一個網站,然后單擊兩個按鈕。 第一個按鈕單擊並通過,但是第二個按鈕發出該錯誤
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();
錯誤:
(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.
我不確定如何處理JS中的錯誤,我仍在學習。 有人可以解釋嗎?
在selenium driver.wait
返回IThenable
或Promise
。 承諾只是用javascript進行異步編程的一種方法,它們有兩個函數, then
`catch',最新的是您如何處理承諾中的拒絕 (錯誤)。 因此,您的代碼必須類似於:
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
});
為了進一步參考,我覺得這個文章是一個很好的介紹,以承諾。
更新
您的問題最有可能是因為您試圖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
});
注意:這僅適用於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:因為您說您還在學習,所以我認為這里有些術語可能不知道這是否成立,因此建議您進行研究。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.