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

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
rajpradeep32
  • 39
  • 1
  • 7
  • 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. – alecxe Oct 02 '15 at 13:44
  • How to get the text ' caught Hughes' then?? – rajpradeep32 Oct 02 '15 at 13:48
  • 12.6 caught Hughes 73/4
    – rajpradeep32 Oct 02 '15 at 13:48
  • 1
    Maybe you should try `getText()` on the WebElement like suggested in [here](http://stackoverflow.com/questions/32907161/cannot-get-text-using-text-in-xpath) – Maze Oct 02 '15 at 13:59
  • @alecxe, can you cite a reference for that? I don't doubt that you're right, but if you give a reference we can explore the boundaries of that limitation. – LarsH Oct 02 '15 at 14:21
  • @LarsH cannot find it documented, but some references are [here](https://code.google.com/p/selenium/issues/detail?id=5459#c1) or http://stackoverflow.com/questions/8923721/using-xpath-selector-following-siblingtext-in-selenium-python - from time to time I see similar requests here on SO. Thanks. – alecxe Oct 02 '15 at 14:28
  • 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 http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/dotnet/Selenium.ISelenium.html 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. – LarsH Oct 02 '15 at 14:31
  • it looks duplicate of: http://stackoverflow.com/questions/32808008/how-to-retrieve-the-text-from-an-outer-element-using-selenium-webdriver/32808721?noredirect=1 – Mahsum Akbas Oct 02 '15 at 16:22
  • @ Mahsum Akbas : Not a duplicate, The other question has been updated from what I was seeking earlier. – rajpradeep32 Oct 02 '15 at 17:57
  • Possible duplicate of [XPath: select text node](http://stackoverflow.com/questions/5033955/xpath-select-text-node) – Paul Sweatte Oct 02 '15 at 18:49

2 Answers2

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());
Flow Bo
  • 19
  • 7
  • 2
    This gives me the entire text 12.6 caught Hughes 73/4 but am trying to get the text caught Hughes alone. – rajpradeep32 Oct 02 '15 at 14:17
  • 1
    Welcome to Stack Overflow! While this answer is probably correct and useful, it is preferred if you [include some explanation along with it](http://meta.stackexchange.com/q/114762/159034) 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. – Kevin Brown-Silva Oct 02 '15 at 21:44
0

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

So after reading you should remove integer values from text.

Thanks Sadik

Sadik Ali
  • 1,129
  • 10
  • 26