1

I would like to use getText() for one XPath, need text what is there.

//span(contains(@style,'display:none'))

XPath is working tested in firebug, I've tried getText, getAttribute, so far no luck

Yi Zeng
  • 32,020
  • 13
  • 97
  • 125
Karol
  • 165
  • 2
  • 6
  • 19
  • This link details how to use java's javascriptExecutor method: http://stackoverflow.com/questions/11430773/how-to-use-javascript-with-selenium-webdriver-using-java To assist you further, we'll need to see the HTML, as barak has already noted. – Richard Apr 07 '14 at 15:37

2 Answers2

2

It's a little hard to say without the exact HTML, which you have not specified in your question...

To begin with, you need to change this:

"//span(contains(@style,'display:none'))"

To this:

"//span[contains(@style,'display:none')]"

UPDATE:

Alternatively, since the span element is not visible, you might be able to do it with:

String innerHTML = elem.getAttribute("innerHTML");

Where elem is the parent node of the span element.

Then, in order to get the actual text, you will need to parse the innerHTML string.

barak manos
  • 29,648
  • 10
  • 62
  • 114
  • One thing, Selenium doesn't interact with elements that are not visible. He's likely going to need to use a javascript executor solution. – Richard Apr 07 '14 at 15:29
  • Thank You barak but still I can not getText(). Richard could you please point me to this js solution? – Karol Apr 07 '14 at 15:33
  • @Karol: You might be able to do it with `String innerHTML = elem.getAttribute("innerHTML")`, where `elem` is the parent node of the `span` element. – barak manos Apr 07 '14 at 15:37
  • `getAttribute("innerHTML")` will get eveything inside, which includes HTML for descendant nodes. I reckon `getAttribute("textContent")` is better. – Yi Zeng Apr 07 '14 at 22:28
  • @user1177636: Please note the last sentence in the answer: "In order to get the actual text, you will need to parse the `innerHTML` string". I'm not familiar with the `textContent` attribute, so thank you for the additional info :) – barak manos Apr 07 '14 at 22:39
  • @barakmanos: Actually I need to take back what I've said. Using `textContent` will still need string parsing. The difference between `textContent` and `innerHTML` is about HTML tags. Parsing result from `textContent` will be easier by replacing inner nodes text with empty string without worrying about tags. – Yi Zeng Apr 07 '14 at 23:21
0

Because the element is invisible (it has display:none), Selenium cannot natively interact with it. You need cast your driver to JavascriptExecutor, then execute the following javascript:

$x("//span(contains(@style,'display:none'))")[0].text

The [0] returns the 1st element returned by the xpath.

This will return the inner text of the element.

Nathan Merrill
  • 7,648
  • 5
  • 37
  • 56