How to get xpath of different html values with the same properties - c#

I'm working on Selenium and trying to get the values inside tags. The site that I'm working on is https://www.qnbfinansbank.enpara.com/doviz-kur-bilgileri/doviz-altin-kurlari.aspx. But the properties of the objects are the same. Therefore, the xpath scripts are the same. The values that I'm trying to get are like 5,615505 TL, 4,827450 TL, 187,389825 TL from
<div class="dlCont">
<span>5,615505 TL </span>
</div>
<div class="dlCont">
<span>4,827450 TL </span>
</div>
<div class="dlCont">
<span>187,389825 TL </span>
</div>
and so on. Is there any way to get the xpath of these values?

You can store all the values in a List. Then one by one you can retrieve it.
Something like :
IList<IWebElement> allValues= driver.FindElements(By.CssSelector("div.dlCont span"));
foreach (IWebElement values in allValues)
{
Console.WriteLine(values.Text);
}
Hope this will help.

You can use like this,
//span[contains(text(),'5,615505 TL')]

You can manually write the xpath for the below DOM Structure
<div class="dlCont">
<span>5,615505 TL </span>
</div>
Manually written xpath for above DOM structure is "//div[#class='dlCont']/span".
if the page is having many elements with same DOM struture then written Xpath will match with all the nodes.
There are 8 nodes are matched with XPATH="//div[#class='dlCont']/span" in the below URL https://www.qnbfinansbank.enpara.com/doviz-kur-bilgileri/doviz-altin-kurlari.aspx
if you want to fetch particular webelements then you need to specify the index value as "(//div[#class='dlCont']/span)[2]".
you need to add open bracket in the starting of the manually written xpath and close bracket in the ending of the Xpath.after that you need to mention the index value
1.//div[#class='dlCont']/span
2.(//div[#class='dlCont']/span
3.(//div[#class='dlCont']/span)
4.(//div[#class='dlCont']/span)[1]
Hope it will be helpful

Related

Selenium C#: Get Element by title

I want to get an element by its title, I don't know if we have a universal solution for this in Selenium C#.
Here is the example of the HTML
<div class="student">
<a href="abcxyz.com">
<span class="title"> ZZZ</span>
</a>
</div>
And another example
<div class="page">
</div>
Of course if just one page, I will do it like
FindElement(By.ClassName("page")).GetAttribute("href");
or
FindElement(By.ClassName("student")).GetAttribute("href")
to get the "href" out of where the "title" is located. But they have pages with different designs, and the only common thing between them is that title. I wonder if there is any solution for me to find the "href" by the "title=ZZZ"?
Thanks in advance.
The two examples are different, I would use a union (|) operator of XPath expressions that will work in both cases. If you have more cases, you can add additional union operator, followed by the new case XPath:
//a[#title='ZZZ']/#href | //*[#class='title'][contains(text(),'ZZZ')]/parent::a/#href
Without searching for specific text:
//a[#title]/#href | //*[#class='title']/parent::a/#href
Try this xpath:
/a[#title="ZZZ"]/#href

How to get values of same class/properties in Selenium C#?

I am currently automating tests to compare expected and actual endorsements on a summary page.
How can I read all endorsements values on the page shown on the summary page.These can change, meaning there can be 2-5 depending on different input. I have tried Xpath and CSS selector but have had no luck. Here are the elements properties for two endorsements the rest of the endorsements will have same properties (elements wise) just different values.
I want to be able to get all the endorsements listed on the page so I can input to my excel sheet for comparison against expected endorsement.
ENDORSEMENT 1:
<div class="guidance smaller ng-scope" ng-repeat="end in
prop.Endorsements">
<a ng-href="#c03770af-3724-4c3a-a240-e341c0d2c3ef" ng-bind-
html="end.Name" class="ng-binding" href="#c03770af-3724-4c3a-
a240-e341c0d2c3ef">Restricted Theft</a>
</div>
<a ng-href="#c03770af-3724-4c3a-a240-e341c0d2c3ef" ng-bind-
html="end.Name" class="ng-binding" href="#c03770af-3724-4c3a-a240-
e341c0d2c3ef">Restricted Theft</a>
ENDORSEMENT 2:
<div class="guidance smaller ng-scope" ng-repeat="end in
prop.Endorsements">
<a ng-href="#93ff9067-f64c-4879-933d-8b0a1d077e74" ng-bind-
html="end.Name" class="ng-binding" href="#93ff9067-f64c-4879-933d-
8b0a1d077e74">Malicious Damage Exclusion</a>
</div>
<a ng-href="#93ff9067-f64c-4879-933d-8b0a1d077e74" ng-bind-
html="end.Name" class="ng-binding" href="#93ff9067-f64c-4879-933d-
8b0a1d077e74">Malicious Damage Exclusion</a>
You need a XPath expression to catch all the a elements at once and store them in a list.
When there are no other anchor tags then the Endorsements:
IList<IWebElement> listOfEndorsements= Driver.FindElements(By.XPath("//a"));
When there are other kind of anchor tags you can try:
IList<IWebElement> listOfEndorsements= Driver.FindElements(By.XPath("//div[contains(#ng-repeat,'prop.Endorsements')]/a"));
Then you can use a ForEach loop to extract from the list of IWebElements the information you need.Like:
foreach (var endorsement in listOfEndorsements)
{
var text = endorsement.Text;
}

C# Selenium Extract from Div and 2 nested Spans.

I'm trying to get the right syntax with either XPATH or CssSelector using C# to extract the dollar amount shown below:
<div class="a-column a-span3 a-text-right a-span-last a-color-price a-text-bold sc-value">
<span class="a-size-base sc-price-sign">
<span class="a-nowrap">$39.93</span>
</span>
</div>
Your help would be greatly appreciated. Thank you.
There are many ways to match the element with the desired text in this case. Here is one of the approaches relying on more or less non-layout specific classes that have a relevant meaning:
driver.FindElement(By.CssSelector(".a-color-price > .sc-price-sign > span")).Text;

How Use From HTMLAgilityPack For Finding A String in nested divs

there is an html codes like below :
<div class="class1 class2 class3">
<div class="class4 class5">
<span class="class6">GOAL STRING</span>
</div>
</div>
now i want to find that GOAL STRING use from HTMLAgilityPack.
how can i do that?
[with LINQ and without LINQ = please show us both ways]
thanks in advance
Well you can use xpath to get the span directly.
document.DocumentNode.SelectSingleNode("//div[#class='class1 class2 class3']/div[#class='class4 class5']/span[#class='class6']").InnerText;
This is a good resource for xpath specifically the table in the middle of the page:
http://www.codeproject.com/Articles/9494/Manipulate-XML-data-with-XPath-and-XmlDocument-C
Also on Google Chrome you can right click -> inspect element and then right click the element that shows up on the tree and click copy as Xpath to get a starting point. These expressions can usually be simplified.

Write query to parse HTML DOCUMENT with HtmlAgilityPack

I want to get the A href of that element in span class="floatClear" whose rating is minimum in
span class="star-img stars_4"
How can I use HtmlAgilityPack to achieve this behaviour I have give the html source of my file
<div class="businessresult"> //will repeat
<div class="rightcol">
<div class="rating">
<span class="star-img stars_4">
<img height="325" width="84" src="http://media1.px" alt="4.0 star rating" **title**="4.0 star rating">
</span>
</div>
</div>
<span class="floatClear">
<a class="ybtn btn-y-s" href="/writeareview/biz/KaBw8UEm8u6war_loc%NY">
</span>
</div>
The query I have written
var lowestreview =
from main in htmlDoc.DocumentNode.SelectNodes("//div[#class='rightcol']")
from rating in htmlDoc.DocumentNode.SelectNodes("//div[#class='rating']")
from ratingspan in htmlDoc.DocumentNode.SelectNodes("//span[#class='star-img stars_4']")
from floatClear in htmlDoc.DocumentNode.SelectNodes("//span[#class='floatClear']")
select new { Rate = ratingspan.InnerText, AHref = floatClear.InnerHtml };
But I do not know how to apply condition here at last line of LINQ query!
Don't select "rating" from the entire htmlDoc, select it from the previously found "main".
I guess you need something like:
var lowestreview =
from main in htmlDoc.DocumentNode.SelectNodes("//div[#class='rightcol']")
from rating in main.SelectNodes("//div[#class='rating']")
from ratingspan in rating.SelectNodes("//span[#class='star-img stars_4']")
from floatClear in ratingspan.SelectNodes("//span[#class='floatClear']")
select new { Rate = ratingspan.InnerText, AHref = floatClear.InnerHtml };
I hope it will not crash if some of those divs ans spans are not present: a previous version of the HtmlAgilityPack returned null instead of an empty list when the SelectNodes didn't find anything.
EDIT
You probably also need to change the "xpath query" for the inner selects: change the "//" into ".//" (extra . at the beginning) to signal that you really want a subnode. If the AgilityPack works the same as regular XML-XPath (I'm not 100% sure) then a "//" at the beginning will search from the root of the document, even if you specify it from a subnode. A ".//" will always search from the node you are searching from.
A main.SelectNodes("//div[#class='rating']") will (probably) also find <div class="rating">s outside the <div class="rightcol"> you found in the previous line.
A main.SelectNodes(".//div[#class='rating']") should fix that.

Categories

Resources