Manu K Mohan Manu K Mohan - 4 months ago 41
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

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>

Answer

The function you want is getText().

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