How to get a span's text that contains “ ” using Selenium
I am using Selenium ChromeDriver. I have a problem obtaining the contents of a span
as defined in the screen shot below:
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?
c# selenium-webdriver
add a comment |
I am using Selenium ChromeDriver. I have a problem obtaining the contents of a span
as defined in the screen shot below:
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?
c# selenium-webdriver
add a comment |
I am using Selenium ChromeDriver. I have a problem obtaining the contents of a span
as defined in the screen shot below:
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?
c# selenium-webdriver
I am using Selenium ChromeDriver. I have a problem obtaining the contents of a span
as defined in the screen shot below:
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?
c# selenium-webdriver
c# selenium-webdriver
edited Dec 31 '18 at 0:21
Dale Burrell
3,13332351
3,13332351
asked Dec 30 '18 at 17:12
alien manalien man
11
11
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
Thank for your help. But return same result jdmac020
– alien man
Dec 30 '18 at 17:32
@alienman Could it be the "value" attribute and not text?
– jdmac020
Dec 30 '18 at 17:41
Hi jdmac020, I tried: element.Text; element.GetAttribute("innerText"); element.GetAttribute("text"); element.GetAttribute("value"); But not work for this element. <span class="choice7 group1 drag no7 yui3-dd-draggable unplaced" style="width: 376px; height: 29px; line-height: 29px; visibility: visible; position: absolute; left: 461.156px; top: 770px;" id="yui_3_15_0_3_1546192163484_384">Yes, we can both go.</span>
– alien man
Dec 30 '18 at 17:49
@alienman how about the "innerHtml" attribute instead?
– jdmac020
Dec 30 '18 at 17:56
@alienman alternately, maybe the "textContent" attribute is a better fit?
– jdmac020
Dec 30 '18 at 18:07
|
show 2 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53979730%2fhow-to-get-a-spans-text-that-contains-nbsp-using-selenium%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Thank for your help. But return same result jdmac020
– alien man
Dec 30 '18 at 17:32
@alienman Could it be the "value" attribute and not text?
– jdmac020
Dec 30 '18 at 17:41
Hi jdmac020, I tried: element.Text; element.GetAttribute("innerText"); element.GetAttribute("text"); element.GetAttribute("value"); But not work for this element. <span class="choice7 group1 drag no7 yui3-dd-draggable unplaced" style="width: 376px; height: 29px; line-height: 29px; visibility: visible; position: absolute; left: 461.156px; top: 770px;" id="yui_3_15_0_3_1546192163484_384">Yes, we can both go.</span>
– alien man
Dec 30 '18 at 17:49
@alienman how about the "innerHtml" attribute instead?
– jdmac020
Dec 30 '18 at 17:56
@alienman alternately, maybe the "textContent" attribute is a better fit?
– jdmac020
Dec 30 '18 at 18:07
|
show 2 more comments
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.
Thank for your help. But return same result jdmac020
– alien man
Dec 30 '18 at 17:32
@alienman Could it be the "value" attribute and not text?
– jdmac020
Dec 30 '18 at 17:41
Hi jdmac020, I tried: element.Text; element.GetAttribute("innerText"); element.GetAttribute("text"); element.GetAttribute("value"); But not work for this element. <span class="choice7 group1 drag no7 yui3-dd-draggable unplaced" style="width: 376px; height: 29px; line-height: 29px; visibility: visible; position: absolute; left: 461.156px; top: 770px;" id="yui_3_15_0_3_1546192163484_384">Yes, we can both go.</span>
– alien man
Dec 30 '18 at 17:49
@alienman how about the "innerHtml" attribute instead?
– jdmac020
Dec 30 '18 at 17:56
@alienman alternately, maybe the "textContent" attribute is a better fit?
– jdmac020
Dec 30 '18 at 18:07
|
show 2 more comments
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.
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.
edited Dec 30 '18 at 20:49
answered Dec 30 '18 at 17:23
jdmac020jdmac020
320413
320413
Thank for your help. But return same result jdmac020
– alien man
Dec 30 '18 at 17:32
@alienman Could it be the "value" attribute and not text?
– jdmac020
Dec 30 '18 at 17:41
Hi jdmac020, I tried: element.Text; element.GetAttribute("innerText"); element.GetAttribute("text"); element.GetAttribute("value"); But not work for this element. <span class="choice7 group1 drag no7 yui3-dd-draggable unplaced" style="width: 376px; height: 29px; line-height: 29px; visibility: visible; position: absolute; left: 461.156px; top: 770px;" id="yui_3_15_0_3_1546192163484_384">Yes, we can both go.</span>
– alien man
Dec 30 '18 at 17:49
@alienman how about the "innerHtml" attribute instead?
– jdmac020
Dec 30 '18 at 17:56
@alienman alternately, maybe the "textContent" attribute is a better fit?
– jdmac020
Dec 30 '18 at 18:07
|
show 2 more comments
Thank for your help. But return same result jdmac020
– alien man
Dec 30 '18 at 17:32
@alienman Could it be the "value" attribute and not text?
– jdmac020
Dec 30 '18 at 17:41
Hi jdmac020, I tried: element.Text; element.GetAttribute("innerText"); element.GetAttribute("text"); element.GetAttribute("value"); But not work for this element. <span class="choice7 group1 drag no7 yui3-dd-draggable unplaced" style="width: 376px; height: 29px; line-height: 29px; visibility: visible; position: absolute; left: 461.156px; top: 770px;" id="yui_3_15_0_3_1546192163484_384">Yes, we can both go.</span>
– alien man
Dec 30 '18 at 17:49
@alienman how about the "innerHtml" attribute instead?
– jdmac020
Dec 30 '18 at 17:56
@alienman alternately, maybe the "textContent" attribute is a better fit?
– jdmac020
Dec 30 '18 at 18:07
Thank for your help. But return same result jdmac020
– alien man
Dec 30 '18 at 17:32
Thank for your help. But return same result jdmac020
– alien man
Dec 30 '18 at 17:32
@alienman Could it be the "value" attribute and not text?
– jdmac020
Dec 30 '18 at 17:41
@alienman Could it be the "value" attribute and not text?
– jdmac020
Dec 30 '18 at 17:41
Hi jdmac020, I tried: element.Text; element.GetAttribute("innerText"); element.GetAttribute("text"); element.GetAttribute("value"); But not work for this element. <span class="choice7 group1 drag no7 yui3-dd-draggable unplaced" style="width: 376px; height: 29px; line-height: 29px; visibility: visible; position: absolute; left: 461.156px; top: 770px;" id="yui_3_15_0_3_1546192163484_384">Yes, we can both go.</span>
– alien man
Dec 30 '18 at 17:49
Hi jdmac020, I tried: element.Text; element.GetAttribute("innerText"); element.GetAttribute("text"); element.GetAttribute("value"); But not work for this element. <span class="choice7 group1 drag no7 yui3-dd-draggable unplaced" style="width: 376px; height: 29px; line-height: 29px; visibility: visible; position: absolute; left: 461.156px; top: 770px;" id="yui_3_15_0_3_1546192163484_384">Yes, we can both go.</span>
– alien man
Dec 30 '18 at 17:49
@alienman how about the "innerHtml" attribute instead?
– jdmac020
Dec 30 '18 at 17:56
@alienman how about the "innerHtml" attribute instead?
– jdmac020
Dec 30 '18 at 17:56
@alienman alternately, maybe the "textContent" attribute is a better fit?
– jdmac020
Dec 30 '18 at 18:07
@alienman alternately, maybe the "textContent" attribute is a better fit?
– jdmac020
Dec 30 '18 at 18:07
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53979730%2fhow-to-get-a-spans-text-that-contains-nbsp-using-selenium%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown