Skip to content Skip to sidebar Skip to footer

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.xpa

Solution 1:

The function you want is getText().

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

Solution 2:

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.


Solution 3:

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

This will return the text within that element.


Solution 4:

var element = driver.findElement(By.xpath("xpath")).getText();

console.log(element);

Not tested, but this will do.


Post a Comment for "Get Text From XPath Located Element Using Selenium WebDriver With JavaScript"