Manu K Mohan Manu K Mohan - 3 months ago 30x
Javascript Question

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


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();