Stephen Gross
Stephen Gross

Reputation: 5724

Using Selenium, xpath gets tags but text is empty

I'm using Selenium WebDriver to fetch a bunch of tags via XPATH. The xpath successfully finds the tags, but the "Text" attribute for the returned IWebElements is empty.

Here's the HTML:

<ul>
<li id="foo1">someValue</li>
<li id="foo2">someOtherValue</li>
</ul>

And the xpath:

//ul/li[startswith(@id, 'foo')]

Any ideas? The Xpath definitely grabs the right elements, but the Text element is empty.

Upvotes: 0

Views: 2028

Answers (1)

Surya
Surya

Reputation: 4536

Try this code to get the text of 2nd element - <li id="foo2">someOtherValue</li> :

WebElement fooEle = driver.findElement(By.xpath("//descendant::ul/li[starts-with(@id,'foo')][2]"));
String fooEleText = fooEle.getText();
System.out.println("foo Element Text -" + fooEleText); //should print expected text "someOtherValue"

Note- if you want to get the 1st element text, then change the index value of the xpath That is, //descendant::ul/li[starts-with(@id,'foo')][1]

Upvotes: 1

Related Questions