I'm trying to make JS load a website and then click on two buttons. The first button clicks and passes, but the second one shoots out this error
const ATC_Button = driver.wait(
webdriver.until.elementLocated({ name: 'mit' }),
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. Can someone explain?
I'm trying to make JS load a website and then click on two buttons. The first button clicks and passes, but the second one shoots out this error
const ATC_Button = driver.wait(
webdriver.until.elementLocated({ name: 'mit' }),
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. Can someone explain?
Share Improve this question edited May 25, 2018 at 14:38 Marcos Casagrande 40.5k11 gold badges93 silver badges106 bronze badges asked May 25, 2018 at 14:37 SunstroSunstro 752 gold badges4 silver badges9 bronze badges 3- Yes. You're webdriver seems to be trying to access the DOM before it's loaded. Side note: this is way, way, WAY over your head if you're just learning. Start by making a webapp or a mand-line utility or something. – Jared Smith Commented May 25, 2018 at 14:44
- @JaredSmith Any good reference material on where to get started? The documentation isn’t very helpful and every tutorial is outdated. – Sunstro Commented May 27, 2018 at 15:58
- Get started doing what? The problem is that all of the documentation/tutorials are going to assume that you know things you don't yet know: they we're written for professional programmers not beginners. This is not a beginner task. Which is fine, we've all got to start somewhere. I mean, I can link you to some documentation on promises like the answer you accepted but that doesn't tell you why the rejection occured, and the documentation expects that you already know that you e.g. have to wait for the DOM to load before you look for an element. – Jared Smith Commented May 28, 2018 at 2:55
1 Answer
Reset to default 5In selenium driver.wait
returns an IThenable
or 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. So, your code needs to be something like:
const ATC_Button = driver.wait(
webdriver.until.elementLocated({ name: 'mit' }),
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:
1. Promise.then
driver
.wait(webdriver.until.elementLocated({ name: "mit" }), 20000)
.then(button => button.click()) // Here the WebElement has been returned
.catch(function(err) {
// Do something with you error
});
2. await syntax
Note: This is only available for ES6
// Here you are waiting for the promise to return a value
const ATC_Button = await driver
.wait(webdriver.until.elementLocated({ name: "mit" }), 20000)
.catch(function(err) {
// Do something with you error
});
ATC_Button.click();
p.s.: Since you say you are still learning I assume there are terms here that might not know if that is true I remend you to do research.