XPath query not working(need find by text) - c#

Hello sow i working with HtmlAgilityPack and i have this problem all elemnts that i need have the same stractior and the same class exept the text of the span like in the code i have span with text Amount and Date sow i need to build link like this
"//span(with text=Amount)[div and contains(#class,'detailsValue ')]");
I need to get data 1,700,000.00 from the div that in the span with text 'Amount' and 14.04.2014 from the div that in the span with text 'Date'
Any ideas?
This what i have now
List<string> OriginalAmount = GetListDataFromHtmlSourse(PageData, "//span[div and contains(#class,'detailsValue ')]");
private static List<string> GetListDataFromHtmlSourse(string HtmlSourse, string link)
{
List<string> data = new List<string>();
HtmlAgilityPack.HtmlDocument DocToParse = new HtmlAgilityPack.HtmlDocument();
DocToParse.LoadHtml(HtmlSourse);
foreach (HtmlNode node in DocToParse.DocumentNode.SelectNodes(link))
{
if (node.InnerText != null) data.Add(node.InnerText);
}
return data;
}
<div class=" depositDetails cellHeight float " style="height: 37px;">
<span class=" detailsName darkgray ">Amount</span>
<br>
<div class="detailsValue float" style="direction:rtl">1,700,000.00 </div>
</div>
</div>
<div class="BoxCellHeight float">
<div class="cellHeight separatorvertical float" style="height: 46px;"> </div>
<div class=" depositDetails cellHeight float " style="height: 40px;">
<span class=" detailsName darkgray ">Date</span>
<br>
<div class="detailsValue float">14.04.2014</div>
</div>
</div>

Actually, the question is not very clear. How about this :
//span[.='Amount']/following-sibling::div[contains(#class,'detailsValue')]]
Above XPath will search for <span> element with text equals "Amount", then get it's following <div> sibling having class contains "detailsValue"
UPDATE :
According to your comment, if I don't misunderstand it, you want both value (div after Amount span and div after Date span). Try this XPath :
//span[.='Amount' or .='Date']/following-sibling::div[contains(#class, 'detailsValue')]

Related

Selenium get element in C#

I'm trying to get number in small tag in HTML
Html code:
...
<div class="form-group col-sm-12">
<label>Amount</label>
<input type="number" name="amount" class="form-control" placeholder="10" value="10">
<small class="form-text text-muted">Max Amount: 2000</small>
</div>
...
this code I have written returns null.
I want to get 2000 number in the element.
please try below solution
WebElement element = driver.FindElement(By.XPath("//div[#class='form-group col-sm-12']/small"))
String elementText=element.Text;
Console.WriteLine("Text", Regex.Replace(elementText, "[^a-zA-Z]+", ""));
You don't necessarily need regex for this:
WebElement ele= driver.FindElement(By.XPath("//small[#class='form-class text-muted']"))
String eleText = ele.Text;
int eleVal = 0;
int.tryParse(eleText, out eleVal);

Selenium C# List

Hello Stackoverflow Users,
I have a internet site with 99 list elements.
The diffrence between the elements are only the names.
<li class="_6e4x5">
<div class="_npuc5">
<div class="_f5wpw">
<div class="_eryrc">
<div class="_2nunc">
<a class="_2g7d5 notranslate _o5iw8" title="Name1" href="/"Name1/">"Name1</a>
</div>
</div>
</div>
</div>
</li>
[...]
<li class="_6e4x5">
<div class="_npuc5">
<div class="_f5wpw">
<div class="_eryrc">
<div class="_2nunc">
<a class="_2g7d5 notranslate _o5iw8" title="Name99" href="/"Name99/">"Name99</a>
</div>
</div>
</div>
</div>
</li>
What I want:
I want to take the "title" of each list element and put it in a new list.
What I tried:
List<string> following = new List<string>();
By name = By.XPath("//div[#class='_2nunc']");
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
IList<IWebElement> displayedOptions = driver.FindElements(name);
foreach (IWebElement option in displayedOptions)
{
string temp = displayedOptions[i].ToString();
following.Add(temp);
i++;
}
If I run the code, I only get the element ID, and not the "title" (name34 for example). I hope you have enough information to help me with my problem. Thanks in advance for every help!
To take the title of each list element and put it in a new list you can use the following code block :
List<string> following = new List<string>();
IList<IWebElement> displayedOptions = driver.FindElements(By.XPath("//li[#class='_6e4x5']//a[#class='_2g7d5 notranslate _o5iw8']"));
foreach (IWebElement option in displayedOptions)
{
string temp = option.GetAttribute("title");
following.Add(temp);
}
You're looking to get the a element's title attribute. The selenium IWebElement interface has a GetAttribute method you can use to get the title of your elements.
foreach (IWebElement option in displayedOptions)
{
following.Add(option.GetAttribute("title"));
}

Get all elements in a NodeCollections

I have an html file :
<div class="form-wrapper">
<div></div>
<div class="Clearfix">
<div></div>
<div></div>
<span></span><span class="time">Time</span>
</div>
<div></div>
<div class="Clearfix">
<div></div>
<div></div>
<span></span><span class="time">Time1</span>
</div>
<div></div>
<div class="Clearfix">
<div></div>
<div></div>
<span></span><span class="time">Time2</span>
</div><div></div>
<div class="Clearfix">
<div></div>
<div></div>
<span></span><span class="time">Time3</span>
</div>
I'm using the c# code below to get all the times items :
var node_1 = htmlDocument.DocumentNode.SelectNodes("//div[#class='form-wrapper']").First();
var ITEM = node_1.SelectNodes("//div[#class='clearfix']");
for (int Node = 0; Node < ITEM.Count; Node++)
{
Console.WriteLine(ITEM[Node].SelectNodes("//span[#class='time']")[1].InnerText.Trim());
}
Console.ReadKey();
I'm taking the First() "Form-wrapper" since they're many .
I tried to use this too :
foreach (var Node in node_1.SelectNodes("//div[#class='clearfix']"))
{
//
}
Issue is : as you can see I have 4 Clearfix Classes so i need to get the result :
Time
Time1
Time2
Time3
but for some reasons i only get :
Time
Time
Time
Time
When you are querying over some node you don't need // at the beginning, if you are adding it query will be executed over whole document.
You need to take first node after selecting, so you need to take node with index 0 not 1
This 2 points will solve your problem, but there are some improvements you can do
Instead of SelecNodes().First() you can user SelectSingleNode()
If you don't need any information about parent nodes you can directly query
for child nodes - htmlDocument.SelectNodes("\\span[#class='time']") will do all the work

Selenium C# unable to get the list using FindElements

0 ,C# and NUnit.
The problem I am facing is that I need to fetch out the value from a span and store into an arraylist.
I am Using the below code to fetch the price list
var productprice = driver.FindElements(By.CssSelector(".price.mb-10.text-right "));
from the HTML given below
<div class="pl-sec">
<ul>
<li class="row-fluid">
<li class="row-fluid">
<div class="span2">
<div class="span7 disc-content">
<div class="span3 price-content">
<div class="price mb-10 text-right"> £14.99</div>
<div class="full-width mb-20">
</div>
</li>
<li class="row-fluid">
<div class="span2">
<div class="span7 disc-content">
<div class="span3 price-content">
<div class="price mb-10 text-right"> £16.99</div>
<div class="full-width mb-20">
</div>
</li>
I do get the list but am unable to fetch the text from the elements I got using the css selector.
Below is the watch from visual studio.
- [0] {OpenQA.Selenium.Firefox.FirefoxWebElement} OpenQA.Selenium.IWebElement {OpenQA.Selenium.Firefox.FirefoxWebElement}
+ [OpenQA.Selenium.Firefox.FirefoxWebElement] {OpenQA.Selenium.Firefox.FirefoxWebElement} OpenQA.Selenium.Firefox.FirefoxWebElement
Displayed true bool
Enabled true bool
+ Location {X = 802 Y = 793} System.Drawing.Point
Selected false bool
+ Size {Width = 164 Height = 14} System.Drawing.Size
TagName "div" string
Text "£16.99" string
I need to fetch the 'Text' with price given in the above watch.
Sorry if its unclear .I am a bit new to this.
Finally Fixed it.
The issue was that the var used to store elements stored elements as an arraylist and had to find the position and then dig into the Text property.
Below is the corrected code-
driver.FindElement(By.XPath("//div[4]/div[2]/ul/li[2]/label")).Click();
//This below codes gets the count of product displayed by checking the prince.
var productprice = driver.FindElements(By.CssSelector(".price.mb-10.text-right "));
//var productprice = driver.FindElement(By.XPath("//div[#class='price.mb-10.text-right']")).Text;
string strval = "";
strval = productprice[5].Text;

HtmlAgilityPack issue with HTML

I want to get title, image src and other details but here is an issue
<div class="thumb-container">
<a class="featured" title="Spectacularly " href="http://www.site.com"></a>
<div rel="0" id="property_image_1181140" class="thumb">
<a title="*Want this title*" href="*http://www.wanttogetthislink.com*">
<img style="width: 190px; height: 127px; left: -11px; top: 0px;" alt="Spectacularly upgraded 5 bed Family Villa For Sale" src="http://c1369013.r13.cf3.rackcdn.com/1181140-1-mini.jpg">
</a>
</div>
<div class="description-listing">
<div class="heading">
<div class="type">
<label>*5,900* sq.ft.,</label>
<span>*Villa*</span>
<p class="bedroom"><em>*5*</em></p>
<p class="bathroom"><em>*6*</em></p>
</div>
<p class="amount">
<label>AED</label>
<strong>*5,120,000*</strong>
</p>
</div>
Here is my code
var allCarResults = rootNode.SelectNodes("//div[normalize-space(#class)='general-listing']");
foreach (var carResult in allCarResults)
{
var dataNode = carResult.SelectSingleNode(".//div[#class='thumb']");
var carNameNode = dataNode.SelectSingleNode(".//a");
}
Here i want to get everything in **
i do not know how to do that..
The principle is pretty much the same, you'll need to write an XPath for each item and select it from a common anchor:
HtmlNode thumbContainer = doc.DocumentNode.SelectSingleNode("//div[#class='thumb-container']");
HtmlNode link = thumbContainer.SelectSingleNode("./div[#class='thumb']/a");
string linkTitle = link.Attributes["title"].Value;
string linkHref = link.Attributes["href"].Value;
HtmlNode label = thumbContainer.SelectSingleNode("./div[#class='description-listing']/div[#class='heading']/div[#class='type']/label");
string labelText = label.InnerText;
// ... Similar for other items
Alternatively you could iterate through each HtmlNode and its children, then for each item match it against a list of items you're after.

Categories