2

xpath:

//*[@id='full-scorecard']/div[2]/div/table[1]/tbody/tr[3]/td[2]/child::text()

The above code only returns a webelement am not able to print the text.

WebElement txt= driver.findElement(By.xpath(
   "//*[@id='full-scorecard']/div[2]/div/table[1]/tbody/tr[3]/td[2]/child::text()"));

System.out.println(txt);

HTML:

12.6 caught Hughes 73/4

10
  • In selenium, the XPath expression used inside By.xpath() locator has to point to a web element. You cannot match a text node this way. Commented Oct 2, 2015 at 13:44
  • How to get the text ' caught Hughes' then?? Commented Oct 2, 2015 at 13:48
  • <td colspan="8"> <b>12.6</b> caught Hughes <b>73/4</b> <br/> </td> Commented Oct 2, 2015 at 13:48
  • 1
    Maybe you should try getText() on the WebElement like suggested in here Commented Oct 2, 2015 at 13:59
  • 1
    @alecxe, thanks, that helps. It's not clear whether "web element" always means DOM Element, as opposed to some other type of DOM node. I see at release.seleniumhq.org/selenium-remote-control/0.9.2/doc/dotnet/… that Selenium XPath supports selection of attributes (e.g. //a[contains(@href,'#id1')]/@class), so apparently "web elements" are not always DOM elements. Unfortunately Selenium's documentation is not very systematic or thorough. Commented Oct 2, 2015 at 14:31

2 Answers 2

0

this should work :

WebElement txt= driver.findElement(By.xpath("//*[@id='full-scorecard']/div[2]/div/table[1]/tbody/tr[3]/td[2]"));

System.out.println(txt.getText());
Sign up to request clarification or add additional context in comments.

2 Comments

This gives me the entire text 12.6 caught Hughes 73/4 but am trying to get the text caught Hughes alone.
Welcome to Stack Overflow! While this answer is probably correct and useful, it is preferred if you include some explanation along with it to explain how it helps to solve the problem. This becomes especially useful in the future, if there is a change (possibly unrelated) that causes it to stop working and users need to understand how it once worked.
0

Selenium webdriver return all test under element object including child element.

So after reading you should remove integer values from text.

Thanks Sadik

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.