Error Encountered in Selenium Webdriver [UncheckedPromiseRejectionNotification]

Recently ventured into the realm of Selenium through "Mozilla MDN web docs" and encountered a stumbling block with an error. Seeking assistance to overcome this hurdle.

Attempting to retrieve text attributes of elements like p, title, alert... but facing obstacles in achieving it.

var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;

var driver = new webdriver.Builder()
.forBrowser('chrome')
.build();

driver.get('http://mdn.github.io/learning-area/tools-testing/cross-browser-testing/accessibility/native-keyboard-accessibility.html');


var button = driver.findElement(By.css('button:nth-of-type(1)'));


button.click();


var alert = driver.switchTo().alert();

alert.getText().then(function(text) {
console.log('Alert text is \'' + text + '\'');
})

alert.accept();

    uptimizators-MacBook-Air:study uptimizator$ node quick_test
(node:1941) UnhandledPromiseRejectionWarning: NoSuchAlertError: no such alert
  (Session info: chrome=78.0.3904.97)
    at Object.throwDecodedError (/Users/uptimizator/node_modules/selenium-webdriver/lib/error.js:550:15)
    at parseHttpResponse (/Users/uptimizator/node_modules/selenium-webdriver/lib/http.js:563:13)
    at Executor.execute (/Users/uptimizator/node_modules/selenium-webdriver/lib/http.js:489:26)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async thenableWebDriverProxy.execute (/Users/uptimizator/node_modules/selenium-webdriver/lib/webdriver.js:699:17)
(node:1941) 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:1941) [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.
(node:1941) UnhandledPromiseRejectionWarning: NoSuchAlertError: no such alert
  (Session info: chrome=78.0.3904.97)
    at Object.throwDecodedError (/Users/uptimizator/node_modules/selenium-webdriver/lib/error.js:550:15)
    at parseHttpResponse (/Users/uptimizator/node_modules/selenium-webdriver/lib/http.js:563:13)
    at Executor.execute (/Users/uptimizator/node_modules/selenium-webdriver/lib/http.js:489:26)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async thenableWebDriverProxy.execute (/Users/uptimizator/node_modules/selenium-webdriver/lib/webdriver.js:699:17)
(node:1941) 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: 2)

Answer №1

Most tasks in this code snippet are asynchronous and return a promise, so it is important to use the "await" keyword:

; (async function(){
  await browser.openUrl('http://example.com');

  var button = browser.findElement('.btn-primary');

  await button.click();

  var modal = browser.switchTo().modal();

  var text = await modal.getText()
  console.log('Modal text is \'' + text + '\'');

  await modal.confirm()
})()

It seems like there might be some discrepancies with the documentation, possibly due to changes in node versions.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Javascript to dynamically flash-highlight text on a webpage when it first loads

I am looking to create a special highlight effect that activates when the page loads on specific text. Let's focus on the element with id="highlight me". The highlight should glow twice around the selected container before fading out. For a clear unde ...

Discovering the coordinates of an individual face on a complex geometric shape using Three.js

I have transformed a flat surface into triangles and now I am trying to retrieve the position of each face for animation purposes. However, I seem to be facing some challenges in achieving this. if (intersects.length > 0) { // Check if the closest ...

Issue with ng-change function causing data not to load properly

I have added a data-ng-change='getSubjectsClasswise(classBean.class_id);' in the class <select> tag, but for some reason the subjects are not loading in the subject <select> tag. I have checked everything and it all seems fine, b ...

Vue enables seamless click-and-edit functionality for text input

I am in search of a Vue component that allows for click-and-edit functionality. After discovering this fiddle, I made some modifications. It functions like this: https://i.sstatic.net/bSMPj.gif Access the fiddle here. The issue: Currently, an additiona ...

Locate and retrieve the final character of the class identifier

I need to extract a specific class attribute value from a dynamically generated HTML element using JavaScript. The class name follows a pattern like sf-single-children-*, where the asterisk (*) represents a variable number based on the number of child elem ...

Centering a JavaScript object within a div

I recently delved into the world of JavaScript and decided to experiment with this graphing library. I followed one of their example codes, and now I have a bit of a beginner-level HTML/JS question. This is the code snippet they provided: Upon running t ...

Adding and deleting fields with jQuery class manipulation

A form has been created for product sales. The admin has the capability to add fields for multiple products by clicking on the "Add" button using jQuery, and can remove fields by clicking on the "Remove" button. There is an issue with appending this us ...

The process of dynamically loading text into a paragraph using JQuery

I'm trying to create a dynamic paragraph that changes its content based on the link the mouse hovers over. My plan is to have multiple text files and load the content when needed. While I can access the elements, I am struggling with loading and setti ...

Tips for deleting default text from MUI Autocomplete and TextField on click

I am currently using Material-UI (MUI) Autocomplete feature with a TextField element, and I have a specific behavior that I would like to achieve. Currently, when I click on the search bar, the placeholder text moves to the top of the TextField. However, m ...

I am facing an issue where the text I included is not appearing on the website that

While developing a React version of my online portfolio, I encountered an issue. Once deployed on GitHub pages, the text on my landing and contact pages disappeared. Despite removing the background image thinking it might be hiding the text, the issue pers ...

What is the best method to use ng-repeat in a single location?

I am looking to display the names from the array [{'id' : 0, 'name' : 'first'}, {'id' : 1, 'name' : 'second'}] <li ng-repeat="x in array"><strong>Names:</strong>{{x.name}}</l ...

How can I omit extra fields when using express-validator?

Currently, I am integrating express-validator into my express application and facing an issue with preventing extra fields from being included in POST requests. The main reason for this restriction is that I pass the value of req.body to my ORM for databas ...

Achieving a bottom tab bar display exclusively on default screens within nested stacks in react-navigation

Suppose I have a Tab Navigator where each tab contains nested stack navigators (refer to the code snippet below). My goal is to have the tab bar visible only on the default screen of each nested stack, similar to how the current Twitter Android app operate ...

What is the best way to display a child component inside an iframe using Vue.js?

Looking to provide a live preview of an email before sending it out, I've opted to use an iframe to contain the preview and prevent style leaks. The goal is for the preview to update dynamically as the user fills out form details. How can I display a ...

Surprising behavior encountered while utilizing fsPromises.open with Node.js

As I work on a larger app, I encounter an issue with a file writing operation. I am utilizing fsPromises to generate an autosave file, but the path variable seems to lose its value between a console log for debugging and the actual call to open the file. I ...

The Connect method does not trigger upon user connection to the Hub class

My hub class named Dashboard looks like this: public class Dashboard : Hub, IDisconnect, IConnected { //Initializing Repositories private IProfileRepository profileRepository; private ISiteDataRepository siteDataRepository; ...

"Following successful POST login and session storage in MongoDB, the session is unable to be accessed due

When sending login data by POST with the credentials: 'include' option from client server 5500 to backend server 3000, I ensure that my session data is properly stored in MongoDB thanks to the use of 'connect-mongodb-session'. In the ba ...

Calculating remaining change with the modulus operator in JavaScript

I have a task where I need to convert any given number of pennies into the correct amount of Quarters, Dimes, Nickels, and leftover pennies. My program currently uses Math.floor() to calculate the total value of each respective coin type when executed in ...

Tips for typing the following object/type in TypeScript

I need help with typing a user state in my application. I have the following: a user type which includes id, email, firstName, lastName, and verified fields. export type User = { id: string | null; email: string | null; firstName: string | null; l ...

Having trouble displaying an image file in Eclipse

I'm facing an issue where I am unable to view the image file that I have taken a screenshot of using Selenium in Eclipse workspace. Oddly enough, when I copy and paste the link in PC File Explorer, the file does exist. The problem lies in my inability ...