Manu K Mohan Manu K Mohan - 1 year ago 103
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>

Answer Source

The function you want is getText().

String text = driver.findElement(webdriver.By.xpath("//div/span")).getText();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download