Obtain the text content of a WebElement using the Selenium Driver in JavaScript

Is there a way to verify if the element I've selected by its id has the expected text content?

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

var elem = this.driver.findElement(By.id('my_id'));
assert.equal(elem.getText(), 'blablabla');

Unfortunately, the above code is not working as expected:

 AssertionError: ManagedPromise {
   flow_:
    ControlFlow {
      propagateUnhandledRejections_: true,
      activeQueue_:
       TaskQueue {
      == 'blablabla'

I'm unable to find an example demonstrating how to perform this straightforward validation.

Answer №1

The main reason behind this behavior is because elem.getText() returns a Promise, indicating that it will be executed asynchronously with the result available at a later time point. The value returned by getText() is actually a reference to the asynchronous execution process.

In order to effectively utilize the output of getText() after its computation, one should utilize the then method by passing in a function. This function will be triggered once the text has been computed, enabling you to manipulate it (such as comparing it to an anticipated value):

var elem = driver.findElement(By.id('mydiv'));
elem.getText().then(function(s) {
  assert.equal(s, "content");
});

Alternatively, for those who prefer lambda expressions:

elem.getText().then(s => assert.equal(s, "content"));

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

Tips for creating a tooltip above an SVG circle with AngularJS

I'm currently working on a project where I am using AngularJS and SVG to plot circles on a page. My goal is to have a tooltip appear when a user hovers over one of the circles. I came across an explanation on how to achieve this on this website, but u ...

Troubleshooting Puppeteer compatibility issues when using TypeScript and esModuleInterop

When attempting to use puppeteer with TypeScript and setting esModuleInterop=true in tsconfig.json, an error occurs stating puppeteer.launch is not a function If I try to import puppeteer using import * as puppeteer from "puppeteer" My questi ...

Modifying the $locale setting in ui-router

My Goal and Approach Within our AngularJS application, we utilize angular-ui-bootstrap for datepickers. Our aim is to localize the datepickers based on the user's locale. However, dynamically changing the locale in AngularJS poses a challenge due to ...

Next.js React Server Components Problem - "ReactServerComponentsIssue"

Currently grappling with an issue while implementing React Server Components in my Next.js project. The specific error message I'm facing is as follows: Failed to compile ./src\app\components\projects\slider.js ReactServerComponent ...

Three.js fails to render Blender model

After creating a basic blender model, I exported it as a .json file using the three.js plugin. However, when attempting to import the file into my project, I encountered some difficulties. As file transfers are restricted through file://, I uploaded the f ...

Having trouble identifying the issue with the dependent select drop down in my Active Admin setup (Rails 3.2, Active Admin 1.0)

I am currently working on developing a Ruby on Rails application that involves three models: Games that can be categorized into a Sector (referred to as GameSector) and a subsector (known as GameSubsector) A sector consists of multiple subsectors. A Subs ...

Incorporate new content into JavaScript using the input element

I have a unique question - can text be added to JavaScript using the input tag? For example, <input type="text" id="example" /> And let's assume the JavaScript function is: function display_text() { alert("The text entered in #example wi ...

ASP.Net does not support the compilation of AngularJS

Good Evening! I've been struggling with implementing an ASP.NET API, as my angular code seems to be not compiling correctly. I recently set up a new empty web project, installed angular and jquery through NUGET, and now I'm facing issues while ...

Creating an accessible library with webpack and babel integration

Currently, I am in the process of publishing a package on npm (this specific package). The package that I am developing utilizes webpack and babel, with the code written in ES6. Within my source files, there is a file named index.js that exports one of the ...

What could be causing the issue with the focus not being activated when clicking on the input field in Vue?

I am facing an issue where I have to click twice in order to focus on a specific input field, and I'm having trouble setting the cursor at the end of the input. I attempted to use $refs, but it seems like there may be a deeper underlying problem. Any ...

NodeJs is experiencing issues with its routing system and navigation functionalities

In my NodeJs application, I am encountering an issue with the router when trying to navigate to a specific page. The file Register.js is located in the routes folder while server.js is placed in the parent directory. Below is the code snippet: Server.js ...

Deciphering the concept of promises within the Node.js platform

After some research, I have come to understand that there are three main methods of calling asynchronous code: Using Events, for example request.on("event", callback); Callbacks, like fs.open(path, flags, mode, callback); Promises While browsing through ...

Effective ways to resolve the ajax problem of not appearing in the console

I am facing an issue with my simple ajax call in a Java spring boot application. The call is made to a controller method and the returned value should be displayed in the front-end console. However, after running the code, it shows a status of 400 but noth ...

Jest identifies an open handle when working with an Express application

For quite some time now, I've been grappling with a particular issue. It all started when I was conducting basic integration tests using a MongoDB database. However, I've minimized the code to its simplest form. The only thing left running is a s ...

Is it acceptable that req.body is void of content? Should we reconsider this approach?

As a beginner in express, I am working on incorporating a basic login feature into my project. However, I am facing some confusion when it comes to passing data around. There are a few aspects that are perplexing me. I currently store the input value i ...

Experiencing Null Pointer Exception with Selenium WebDriver while utilizing TestNG

I'm a beginner in the world of Selenium and I've encountered a null pointer exception while executing the script below. The test stops running as soon as the site is loaded, and I can't figure out why this exception is being thrown. Here&ap ...

Verify if the arguments of a Discord bot command are in accordance with the format outlined in the commands configuration

I am looking to create a script that verifies if the arguments given for a command align with what the command expects them to be. For instance; When using the config command, the first argument should be either show, set, or reset Additionally, if se ...

There was a glitch encountered while constructing (Verifying type validity) with Prisma

There was an issue in the node_modules/@prisma/client/runtime/library.d.ts file on line 1161, specifically error TS1005 where a '?' was expected. 1161 | select: infer S extends object; | ^ 1162 | } & R ...

Axios sending a 400 Bad Request to Django due to a missing required field

I'm having issues sending a POST request from my React frontend using Axios. import axios from 'axios' axios.post('http://server:port/auth/login', { username: 'admin', password: 'MY_PASSWORD', }, { ...

Issue with Blueimp jquery-file-upload incorrectly displaying file size for jpeg/jpg files over 2MB

While using the blueimg file uploader library available at this link: When I upload a jpeg/jpg image larger than 2MB, it displays the wrong file size. The uploaded file also ends up with the incorrect file size. Could this be a bug in the library or am I ...