Hi I'm kind of new to selenium, so please bear with me if the question is too basic.
I wanna access a date picker element and choose a specific date.
I am trying to access the span element using both class and text inside.
I get an error of invalid string. Is the syntax below correct?
_driver.FindElement(By.CssSelector("span[class='xxx'][contains(text(),'xx')]"))
Looking at this cheatsheet
https://www.simple-talk.com/dotnet/.net-framework/xpath,-css,-dom-and-selenium-the-rosetta-stone/
I think it might be more like
_driver.FindElement(By.CssSelector("span.CCC:contains('TTT')"));
where CCC is your class name and TTT is the text your looking for.
Related
I need to assert if page body contains text "error", how would I do that? I got to this point and I am getting error on .getText
driver.FindElement(By.TagName("body")).getText().Equals("Error")
You don't really want to do it this way... but since you asked, here is the answer.
driver.FindElement(By.TagName("body")).Text.Contains("Error");
You were using .getText() which is Java. You were also using .Equals() but what you meant was .Contains() because the entirety of the body tag is likely not to be only "Error".
What I would recommend is for you to narrow down to the element that actually contains the "Error" text and then use the line above (with the proper locator), e.g.
driver.FindElement(By.Id("the ID of the element that contains Error")).Text.Contains("Error");
You'd have to provide the surrounding HTML for the element for us to help you any more specifically.
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"));
i have the following selfmade definition in on of our customer files:
<bitmaskdef name=SDC_STATUS_BITMASKM_1>
..content
</bitmaskdef>
I had to read out the content between the open and closing tags of the bitmask def.
To achive that, i've used regex in my c# code as following:
var bitmaskDefinitionRegex = new Regex(string.Format(#"<bitmaskdef name={0}>(.*?)</bitmaskdef>", bitmaskName));
bitmaskName contains the name of the bitmask we are searching for, for example: SDC_STATUS_BITMASKM_1
Now our customer wants to change the definition as following:
<bitmaskdef name=SDC_STATUS_BITMASKM_1; SDC_STATUS_BITMASKM_2; SDC_STATUS_BITMASKM_3; SDC_STATUS_BITMASKM_4>
...content
</bitmaskdef>
So that he can set more than one name for the definition. With the current solution it is not able to get the content between the definition. Thats why i thought to change my RegEx but it currently don't know how. Important to know is, that i always only know one of the names.
For example:
Reading out the name SDC_STATUS_BITMASKM_1
Find the definition for the name SDC_STATUS_BITMASKM_1
Reading out the name SDC_STATUS_BITMASKM_2
Find the definition for the name SDC_STATUS_BITMASKM_2
Reading out the name SDC_STATUS_BITMASKM_X
and so on
So far my current solution is not working because i only consider one name, but now i need a solution to find the definition with the content also if there are many other names defined.
Update #1
Here is the way im looking for the regex match. The field "fc" is my file content of the "textfile" which contains the xml like definition.
var bitmaskDefinitionMatch = bitmaskDefinitionRegex.Match(String.Join(String.Empty, fc.ToArray()));
You could use
new Regex(string.Format(#"<bitmaskdef name=[^>]*?{0}(?:>|;[^>]*>)(.*?)</bitmaskdef>", bitmaskName));
It will match any bitmaskdef tag whose name attribute includes bitmaskName, demo here: http://regex101.com/r/lR0kG6
So I'm aware of how to select a node using htmlagilitypack:
HtmlNode.SelectNodes(".//div[#class='description']")
etc... but say I have a site set up in the following way:
This is Link 1
This is information i want to get to
This is Link 3
This is information i want to get to
This is Link 5
This is Link 6
etc...
Now, the snippet is short, but basically, The links are asymmetric, and I only want to access links that have the text value
"this is information i want to get to"
(I'm not familiar enough with hmtl to use proper terminology here, sorry). Is there a method in htmlagilitypack where I can check this text value?
Thank you!
Try using the text() function:
SelectNodes("a[text()='This is information i want to get to']")
I had been trying to extract links from a class called "tim_new" . I have been given a solution as well.
Both the solution, snippet and necessary information is given here
The said XPATH query was "//a[#class='tim_new'], my question is, how did this query differentiate between the first line of the snippet (given in the link above and the second line of the snippet).
More specifically, what is the literal translation (in English) of this XPATH query.
Furthermore, I want to write a few lines of code to extract the text written against NSE:
<div class="FL gL_12 PL10 PT15">BSE: 523395 | NSE: 3MINDIA | ISIN: INE470A01017</div>
Would appreciate help in forming the necessary selection query.
My code is written as:
IEnumerable<string> NSECODE = doc.DocumentNode.SelectSingleNode("//div[#NSE:]");
But this doesnt look right. Would appreciate some help.
The XPath in the first selection reads "select all document elements that have an attribute named class with a value of tim_new". The stuff in brackets is not what you're returning, it's the criteria you're applying to the search.
I don't have the HTML Agility pack, but if you are trying to query the divs that have "NSE:" as its text, your XPath for the second query should just be "//div" then you'll want to filter using LINQ.
Something like
var nodes =
doc.DocumentNode.SelectNodes("//div[text()]").Where(a => a.InnerText.IndexOf("NSE:") > -1);
So in English, "Return all the div elements that immediately contain text to LINQ, then check that the inner text value contains NSE:".
Again, I'm not sure the syntax is perfect, but that's the idea.
The XPath "//div[#NSE:]" would return all divs that have and attribute named, NSE:, which would be illegal anyway because ":" isn't allowed in an attribute name. Youre looking for the text of the element, not one of its attributes.
Hope that helps.'
Note: If you have nested divs that both contain text as in <div>NSE: some text<div>NSE: more text</div></div> you're going to get duplicate results.