Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get text from XPath located Element using Selenium WebDriver with JavaScript

I am trying to find an element using xpath and get the elements text value.

For example, I can find the element using xpath in the following way

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

But I want to get the text of this particular element by using JavaScript.

HTML content is like below

<div><span>Inner Text</span></div>
like image 927
Manu K Mohan Avatar asked Sep 05 '13 12:09

Manu K Mohan


2 Answers

The function you want is getText().

String text = driver.findElement(webdriver.By.xpath("//div/span")).getText();
like image 108
Nathan Merrill Avatar answered Nov 08 '22 11:11

Nathan Merrill


const By = webdriver.By;

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

Most of the actions in the webdriver for node/'javascript' return a promise, you have to do a .then to actually get the values.

like image 4
ShaBANG Avatar answered Nov 08 '22 10:11

ShaBANG