Acquire text from Element located by XPath through the use of Selenium WebDriver in JavaScript

I'm currently attempting to locate an element using xpath and extract its text value.

As an example, I can find the element using xpath using the following method:

driver.findElement(webdriver.By.xpath("//div/span));

However, I am interested in retrieving the text of this specific element using JavaScript.

The HTML content is as follows:

<div><span>Inner Text</span></div>

Answer №1

The specific function you need to use is retrieveText().

String specificText = driver.findElement(webdriver.By.xpath("//div/span")).getText();

Answer №2

const By = webdriver.By;

driver.findElement(By.xpath('//div/span'))
    .then(span => span.getText())
    .then(text => console.log(text))

When using the webdriver in node or JavaScript, many actions return a promise, requiring the use of .then to access the values.

Answer №3

WebDriver webDriver;
String elementText = webDriver.findElement(By.xpath("your xpath")).getText(); 

The code above retrieves the text content of the specified element.

Answer №4

let webpageElement = driver.findElement(By.xpath("xpath")).getText();

console.log(webpageElement);

Although not verified, this code should work.

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

Exploring the world of JavaScript by dynamically retrieving all class functions

Is there a way to retrieve an array of all functions from a given class, including functions inherited from parent classes? For instance: class Foo extends Bar { funcA() {} } class Bar { funcB() {} } const instanceFoo = new Foo(); getClass ...

Enhanced approach to building with React and Express

Quick Summary: How can I set up a project using React on the front-end and Express on the back-end with just one package.json and node_modules folder? When starting a project that combines a React front-end and an Express back-end, my desired structure is ...

Having trouble getting npm install to add any libraries? Wondering how to fix this issue?

Currently, I am using Node version 14.15.5, npm version 6.14.11, and working on VS Code. I attempted to install two packages with the following commands: npm install express npm install lodash Unfortunately, neither installation was successful. This ...

Error encountered while attempting to login to the Winston Logger in the /var/log directory

After hours of attempts, I am still struggling to get Winston to log in my /var/log directory on my Fedora system. I conducted a test project using Express and found that logging works fine within the project directory. However, when attempting to log any ...

Using Python and Selenium for login, encountering difficulty in locating the div element

# _*_coding:utf-8_*_ from selenium import webdriver driver=webdriver.Chrome() url="https://login.alibaba.com" driver.get(url) driver.implicitly_wait(3) print(driver.page_source) driver.quit() Attempting to use Selenium for logging in, but unable to locat ...

How to isolate a function in React when mapping data

I am currently working on mapping data as a repeater in my React project. However, I am facing an issue with isolating the opening function, specifically with an accordion component. As I continue to learn React, I want to ensure that each accordion operat ...

Tips for extracting text content from a <Strong> element using XPath

My HTML code snippet appears below: <p> <strong>Contact Number:</strong> 1(111)656-3485 <br/> ...

The post() method in Express JS is functioning flawlessly in Firebase cloud function after deployment, however, it seems to encounter issues when running on a

https://i.stack.imgur.com/bIbOD.pngI am facing an issue with my Express JS application. Despite having both post() and get() requests, the post() request is not working on my local machine. It keeps throwing a 404 error with the message "Cannot POST / ...

Using Javascript to deactivate a clickable image upon clicking another image

I have two buttons with alert functions assigned to each. I want the function on the first button to only work when that button is clicked, and not work if the second button has been clicked. Below is the code I've tried, but it's not functioning ...

Debugging in IntelliJ IDEA is disabled following the integration of the Browserstack SDK into the selenium test framework with Cucumber Serenity (maven build)

I encountered a problem with debugging after merging my existing Selenium test framework with Browserstack. Prior to the merger, it would pause at breakpoints in the Java code for debugging. However, even when running the same test scenario and checkpoint, ...

Troubleshooting ng-class functionality in AngularJS

I am attempting to utilize AngularJS in order to change the class name of a div element. Despite following the guidance provided in this answer, I am encountering difficulties as the class name is not updating in my view. Below is the code from my view: ...

Direct back to the current page post deleting entry from mongodb

After removing data from MongoDB, how can I redirect to the same view (handlebar)? I tried using res.render, but it shows that the website cannot be reached. Thank you for your assistance. Controller Logic var express = require('express'); va ...

Error: The validation process failed due to missing information. The file name and path are both required for validation

In my current project, I am attempting to upload a file from the frontend to the backend and save it. However, I am encountering an error that looks like this: Error The error message is as follows: this.$__.validationError = new ValidationError(th ...

What is the best way to prioritize loading JSON models first in three.js and declare a variable before initializing?

I am looking to efficiently load multiple JSON models and store them in global variables for easy access. This will streamline tasks like copying, translating, and more without the need to reload a model each time. After trying various methods, I have not ...

What is the best way to apply a conditional binding with v-model in Vue.js?

When working with JavaScript, you can utilize object spreading to include optional values like shown below: const payload = { name: "Joseph", ...(isMember && { credential: true }) }; In React, passing props optionally in JSX is as simple as this: &l ...

Does Protractor 5 work with Selenium version 2.53?

Is it safe to connect Protractor 5 with Selenium webdriver-manager 2.x, or should I stick to using 3.x? ...

Transform [object HTMLDocument] to a String

Could someone guide me in converting the newHTMLDocument object into a full string including the doctype and html tag? let newHTMLDocument = document.implementation.createHTMLDocument(); let html = `<!DOCTYPE html> <html> <head> ...

Tips for managing the most recent keyup event in a search feature

I have a search input on my website. Whenever a user types a letter in it, an ajax request is made to display some content as the result. My goal is to only create an ajax request for the last keyup event when the user types quickly. I came across a soluti ...

Is there any importance to port 9229 and is it possible to modify it?

In my initial training, it was always recommended to run a local Node server on port 3000. However, after learning about the Chrome Node debugger in a tutorial that uses port 9229, I decided to switch to this port. For more information on port 3000, you ...

"Converting a text into a property that can be

In my scenario, I have a set of fixed options along with a dynamic number of yes/no radio inputs named other[index]. By utilizing $(form).serializeArray(), I can obtain an array of name/value objects. Through the use of the reduce method, I am then able to ...