-1

I am using Selenium ChromeDriver. I have a problem obtaining the contents of a span as defined in the screen shot below:

Span I Am Trying To Work With

I tried:

var text = element.Text;

but it returns an empty string.

The span's text is: "Yes, we can both go."

How can I get the span's text?

Dale K
  • 25,246
  • 15
  • 42
  • 71
alien man
  • 1
  • 2

1 Answers1

1

Edited to reflect updates in the comments

Because it's a span, which behaves much like a div element, there's no inherent text or value attribute at play--it's a container, and you'll need to grab the value from inside the span container (see this answer and this article). Based on the fact you want the actual string value and not the html, the "textContent" attribute is probably the best bet.

You can use Selenium's "GetAttribute" method to get the "textContent" attribute of the span:

var textValue = element.GetAttribute("textContent");

// do code against textValue

There's no direct get accessor built into the Element class for attributes, so you'll need to pass in the string value for the name of the attribute you want, and it'll return the value if it's present.

jdmac020
  • 557
  • 1
  • 5
  • 18
  • Thank for your help. But return same result jdmac020 – alien man Dec 30 '18 at 17:32
  • Hi jdmac020, I tried: element.Text; element.GetAttribute("innerText"); element.GetAttribute("text"); element.GetAttribute("value"); But not work for this element. Yes, we can both go. – alien man Dec 30 '18 at 17:49
  • @alienman alternately, maybe the "textContent" attribute is a better fit? – jdmac020 Dec 30 '18 at 18:07
  • Thank you very much, jdmac020. "textContent" solved my problem. – alien man Dec 30 '18 at 18:27
  • @alienman fantastic! Glad to help. I updated the answer to reflect this, including a couple links as to how I found the solution. Feel free to accept the answer so the solution is clear to anyone coming after us. – jdmac020 Dec 30 '18 at 21:11