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>
The function you want is getText()
.
String text = driver.findElement(webdriver.By.xpath("//div/span")).getText();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With