c# Find method returns null when there actually is a match - c#

Maybe I am just too tired but look at this:
var foundTag = bigPins.Where(x => x.Tag == pin.Tag).FirstOrDefault();
You would imagine foundTag to not be null when the list bigPins contains an element with that Tag of pin already.
Now look at this:
You can clearly see that the tag that I am looking for in that list is:
pin.Tag : "282-;215-;price_1K937NB1SsiUQAfn0Tap42d8"
Lets have a look from that exact same stop right there:
Right there under "Tag" it quotes:
Tag : "282-;215-;price_1K937NB1SsiUQAfn0Tap42d8"
and guess what it says under "foundTag"?
It says null.
And therefore this element is added to my list and thats super fun, because that element IS already on that list.
What the hell is going on here?

Related

Having an assertion that contains one word I want it to Assert that word exists anywhere on the page with , or not

I have a Test with an Assert in it for contains Duck.Sometimes I will see Duck,Goose. The later Fails because it sees the ,Goose. I thought Contains would fix this and I thought the * wildcard would fix this but it does not. the Test but I want my assertion to take Duck no matter what else comes after it. I am using C#. I can not find the answer correctly in Google. Here is my code:
string actualvalue
=_driver.FindElementByXPath("//*android.widget.TextView[#text='Duck']").Text;
Assert.IsTrue(actualvalue.Contains("Duck"));
The Xpath needs to be adjusted. Your asterisk won't do the job you are looking for. In your case I believe it should look like this:
"//android.widget.TextView[contains(text(), 'Duck')]"

How Can i get this Xpath

I got the id for capturing.
//button[contains(text(),'Delete')][1]
//button[#id='deletebtn']
but its have 10 duplicate values.can't identify unique thing for capture the element.Please Help me to resolve
As you haven't shared full HTML I am assuming you want use the first locator of 10 matches.
You can use
//(button[contains(text(),'Delete')])[1]
instead of
//button[contains(text(),'Delete')][1]
In case, suppose you want to use an another element then change the match number. Like below,
//(button[contains(text(),'Delete')])[3]
or
//(button[contains(text(),'Delete')])[4]
Note: Selenium by default picks the first element if there are more than one match.
Always check your xPath in chrome console to make sure it is unique.
Press F12 in Chrome.
Go to elements section
Search ( CTRL + F)
Place the xpath and see, if your desired element is getting highlighted with 1/1 matching node. This means, your xPath is unique.
You are using the wrong syntax to write the index based xpath.
Correct syntax for index based XPath-
(//button[contains(text(),'Delete')])[10]
PS: You can verify in SelectorsHub if your xpath is correct or not.

Bug in HTMLAgilityPack when getting href attribute value. C#

Found a nasty bug in HTMLAgilityPack whereby some attribute values are NOT returned fully - they are truncated. Specifically, when attempting to get the href value out of an anchor tag, only the root domain is returned, anything following (the query string) is completely ignored. Anyone know a good workaround?
Example:
node.SelectSingleNode("//link").Attributes["href"].Value
returns https://www.example.com
instead of returning https://www.example.com/mypage.php?_src=ffk_title&ffkid=66534&site=data:http%3A%2F%2Fwww.othersite.com%2Frss%2F
the link looks like so
<a class="tlink" href="https://www.example.com/mypage.php?_src=ffk_title&ffkid=66534&site=data:http%3A%2F%2Fwww.othersite.com%2Frss%2F" target="_blank">Click to get feed</a>
Anyway - right now, I'll just get the link tag and parse with old methods - I figure HTMLAgilityPack gets confused if there are atypical characters in the href tag. I hope it's just something I'm doing wrong, but this kind of quirk is really hurts.
For anchor tags, you should use //a XPath expression:
node.SelectSingleNode("//a").Attributes["href"].Value;
Additionally, if you need to reference an anchor with a particular class, you could use:
node.SelectSingleNode("//a[#class='tlink']").Attributes["href"].Value;
A working example can be seem here.

Passing a value into an Xpath expression

I am currently writing a test against an e-commerce website. When a user runs a search for a particular product a list of items are being returned. What I am hoping to do is pass a particular value (e.g. the number 2) into my test scenario, at which point can be passed into my XPath expression (nth-child), enabling the item to be selected.
The XPath is incorrect and not sure how to fix it. Would appreciate if someone could help.
[Then(#"I select item '(.*)' from the search results")]
public static void WaitAndSelectAnItem(int item)
{
{
Driver.Instance.FindElements(By.CssSelector("#productGrid"));
var itemToSelect = Driver.Instance.FindElement(By.XPath(string.Format("//#class='itemContainer:nth-child({0})'", item)));
itemToSelect.Click();
}
}
Result Message: The given selector //#class='itemContainer:nth-child(3)' is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Unable to locate an element with the xpath expression //#class='itemContainer:nth-child(3)' because of the following error:
TypeError: The expression cannot be converted to return the specified type.
nth-child is not part of the XPath language and is actually a CSS selector pseudo-class.
Instead, you've probably meant:
(//*[#class='itemContainer'])[{0}]
Note that indexing in XPath starts with 1 - make sure you are actually getting the correct item.
Alternatively, you can use findElements() to find all "itemContainers" and then get the desired one by index. Note that it is 0-indexed here:
Driver.Instance.FindElements(By.XPath("//*[#class='itemContainer']"))[item]
Assuming that the "item container" is a DIV tag, if not div replace div by the right tag, * will work as well but there may be chances that you get more elements which you don't want.
Try this:
(//div[#class='itemContainer'])[1]
Replace 1 by your item indices.

Check if element in XML exists that ends with matching string

I can see a way of searching for an element within XML by just going:
if(doc.SelectSingleNode("//mynode")==null)
But what I'm more interested in, is finding an element that matches the part of the name. Something like:
doc.SelectSingleNode ...that contains "table" in it.
So if I had a node called "AlinasTable", I want it to find that. Why it matters is because my node can inconsistently contain anything that comes before "table", like "JohnsTable" - in which case I'd want that to be returned. So something more generic.
Cheers.
You can use the contains function, as in the following XPath expression:
doc.SelectSingleNode("//*[contains(name(), 'Table')]")

Categories

Resources