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?
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.
Related
I m trying to click on value (link format) on a webpage and i read it from a separate file. the value is read correctly from the external file but I cant get the script to click on it. the first line of code reads the value correctly from the external file but the second is supposed to click on the rendered value. So for instance, the text file has one value of X1 and the webpage has a value in link format called X1. So the idea is to click on the X1 link using the variable valueID rather than just reading the text link from the page. any idea how to implement it or get it to work with the code below please?
<pre>
string ValueID = System.IO.File.ReadAllText(#"ValueID.txt");
await _page.ClickAsync("Value=ValueID");
</pre>
The following code should work:
string ValueID = System.IO.File.ReadAllText(#"ValueID.txt");
await _page.ClickAsync($"text={ValueID}");
Notice that you are including the string interpolation operator inside your string, whereas it should be in front of it, e.g. $"text={ValueID}" instead of "text=${ValueID}", but if the above doesn't work, for some reason, also try the following:
when you're reading the value from the text file, make sure there are no leading or trailing whitespace characters
make sure the actual text value of the element does not contain any leading or trailing whitespace characters
is the entire text value of the element exactly equal to the value of your ValueID string? if not, maybe consider using the :has-text() Playwright pseudo-selector, e.g. await _page.ClickAsync($":has-text('{ValueID}')"); which not only is case-insensitive, but also matches on substrings
maybe try using the href attribute paired with a CSS contains instead, e.g. await _page.ClickAsync($"[attribute*={ValueID}]");
I have been working with selenium and C#, and I am wondering if I can return the text associated with a specific element.
For example, here is the line of HTML that contains what I want to display:
<span class="field">1149156-1</span>
Would it be possible to save the "1149156-1" in a string to use for later?
I have tried the following code, but it returns a strange value, definitely not the value I want it to return:
string testvariable = driver.FindElement(By.XPath("/html/body/div/div[4]/div[2]/form/div[2]/div/table/tbody/tr[2]/td[1]")).ToString();
Hope I provided enough information!
You can just use webElement.Text.
string testvariable = driver.FindElement(By.XPath("/html/body/div/div[4]/div[2]/form/div[2]/div/table/tbody/tr[2]/td[1]")).Text;
On another note -- I recommend using relative XPath syntax instead of absolute, and querying on WebElement attributes such as id, name, and class to get more accurate locators.
For example, the XPath in the example you provided can be re-written more robustly as such:
//table[#id='someId']/tbody/tr[2]/td[1]
Here is a link:
<a title = "mylink" href="mysite">content</a>
In AngleSharp object I can easily get content with this code:
string innerContent = item.TextContent;
But I need to get a title of the link and also a href. How do I do that?
Note that AngleSharp uses the standard DOM as defined by the W3C - thus you can just search for, e.g., "how to get href from anchor element in DOM" to retrieve an answer. For completeness, the example search query leads to (first hit on Google) Get local href value from anchor (a) tag, which answers your question.
Just translated to C# that means
var anchor = item as IHtmlAnchorElement; // Assumption: You have obtained it "only" as an IHtmlElement
string title = item.Title;
string href = item.Href;
Remark: There is a difference between .GetAttribute("href") and .Href. The former is always available (even on non-IHtmlAnchorElement) and gives you the real value. The latter is a special computed version available on some elements (e.g., IHtmlAnchorElement) and will get you a normalized version, already considering the base URL of the current document.
TL;DR: .Href will give you an absolute URL while .GetAttribute("href") may give you a relative URL.
HTH!
How to compare text in Ranorex using C# ?
I wants to compare text from a website. In this case we have to compare the text.
Specify the correct validation form of the string:
string validationHasToMatch = "Test data";
Add the element to the repository that contains the text needed for comparison. Let's call the repo object:
repo.DomObject.textElement;
Build a validation around them:
if (repo.DomObject.TextElement.InnerText == validationHasToMatch)
{
Report.Success("Texts match!");
} else {
Report.Failure("Texts don't match!");
}
This is just a quick example and a base to build more functionality ontop of.
the easiest way is to use the innertext property on the element from the website. So for instance use the xpath to find the element, get the innertext property and compare that text in c# the same way you would compare any other string
Im querying a span element (without class, id, or anything) using csspath in selenium c# but the text attribute is empty.
what's happening and how can I get the text within the span?
So the DOM structure is:
/div/div/ul/li/a/span
Query is:
driver.FindElement(By.CssSelector(".contactActions .iconActions .icon .privacy span"));
You could probably try
driver.FindElement(By.XPath("//div[#id='otherOuterDiv']//span"));
I usually use relative XPaths, because I find it easier to specify them.
Solution is:
string text = driver.FindElement(By.CssSelector(".contactActions .iconActions .icon .privacy span")).GetAttribute("textContent"));