Selenium Webdriver: Select radiobutton with specific span title in list - c#

I have the following HTML with in this case 2 items in my ul. Now I want to check (click) the radiobutton where <span title = "SeleniumUpload">. In this HTML there is only one element, but it can be many more.
How do I find this element?
HTML example:
<ul id="ctl00_cpm_AlbumMain_thumbs" class="listAlbums clearfix nospace-bottom" mediastorepartnertype="extrafilm" style="height: 160px;">
<li id="ctl00_cpm_AlbumMain_1da02a93-76cd-46a5-8dfb-e841aedf4398|2ab03151-7f88-4924-9471-248786ba46c0_handle" class="folderitemhandle" pagenr="1">
<a title="Bekijk de foto's in dit album." href="/nl/myaccount/myphotos/album.aspx?albumid=2ab03151-7f88-4924-9471-248786ba46c0">
<img id="ctl00_cpm_AlbumMain_1da02a93-76cd-46a5-8dfb-e841aedf4398|2ab03151-7f88-4924-9471-248786ba46c0_handle_image" class="lazy" width="100" data-original="http://photos.myserver.com.stage/photos/nastemp1/volume1/8889361a-c9e5-4cbd-a5a3-bbf7612dd370/131016/4/8/48f1d74f-0e62-455a-ba60-2a64fc3ff2a6.thumb.jpg?upd=635175161317600000" alt="Bekijk de foto's in dit album." src="http://photos.myserver.com.stage/photos/nastemp1/volume1/8889361a-c9e5-4cbd-a5a3-bbf7612dd370/131016/4/8/48f1d74f-0e62-455a-ba60-2a64fc3ff2a6.thumb.jpg?upd=635175161317600000" style="display: inline;">
</a>
<label class="albumTitle">
<input id="ctl00_cpm_AlbumMain_1da02a93-76cd-46a5-8dfb-e841aedf4398|2ab03151-7f88-4924-9471-248786ba46c0_handle_box" type="radio" onclick="javascript:SelectFolder('ctl00_cpm_AlbumMain_1da02a93-76cd-46a5-8dfb-e841aedf4398|2ab03151-7f88-4924-9471-248786ba46c0_handle','1da02a93-76cd-46a5-8dfb-e841aedf4398|2ab03151-7f88-4924-9471-248786ba46c0');" name="ctl00$cpm$AlbumMain$1da02a93-76cd-46a5-8dfb-e841aedf4398|2ab03151-7f88-4924-9471-248786ba46c0_handle$" value="box">
<span title="Test (2)">Test (2)</span>
</label>
</li>
<li id="ctl00_cpm_AlbumMain_1da02a93-76cd-46a5-8dfb-e841aedf4398|35b9a18d-6203-4fcb-a05f-decb8672d1c7_handle" class="folderitemhandle" pagenr="1">
<a title="Bekijk de foto's in dit album." href="/nl/myaccount/myphotos/album.aspx?albumid=35b9a18d-6203-4fcb-a05f-decb8672d1c7">
<img id="ctl00_cpm_AlbumMain_1da02a93-76cd-46a5-8dfb-e841aedf4398|35b9a18d-6203-4fcb-a05f-decb8672d1c7_handle_image" class="lazy" width="100" data-original="http://photos.myserver.com.stage/photos/nastemp1/volume1/8889361a-c9e5-4cbd-a5a3-bbf7612dd370/131016/7/f/7f2efe77-7754-463e-8eb7-f2853cfa0f07.thumb.jpg?upd=635175162493870000" alt="Bekijk de foto's in dit album." src="http://photos.myserver.com.stage/photos/nastemp1/volume1/8889361a-c9e5-4cbd-a5a3-bbf7612dd370/131016/7/f/7f2efe77-7754-463e-8eb7-f2853cfa0f07.thumb.jpg?upd=635175162493870000" style="display: inline;">
</a>
<label class="albumTitle">
<input id="ctl00_cpm_AlbumMain_1da02a93-76cd-46a5-8dfb-e841aedf4398|35b9a18d-6203-4fcb-a05f-decb8672d1c7_handle_box" type="radio" onclick="javascript:SelectFolder('ctl00_cpm_AlbumMain_1da02a93-76cd-46a5-8dfb-e841aedf4398|35b9a18d-6203-4fcb-a05f-decb8672d1c7_handle','1da02a93-76cd-46a5-8dfb-e841aedf4398|35b9a18d-6203-4fcb-a05f-decb8672d1c7');" name="ctl00$cpm$AlbumMain$1da02a93-76cd-46a5-8dfb-e841aedf4398|35b9a18d-6203-4fcb-a05f-decb8672d1c7_handle$" value="box">
<span title="SeleniumUpload (3)">Selenium... (3)</span>
</label>
</li>
This I have so far, but the title attribute of the span returns an empty string. Why is this?
var albums = driver.FindElements(By.ClassName("albumTitle"));
Console.WriteLine("Found {0} photo-albums", albums.Count);
foreach (var we in albums)
{
var span = driver.FindElement(By.TagName("span"));
String title = span.GetAttribute("title"); // returns empty string, I need this title value
Console.WriteLine("we.text:'{0}' title:''{1}",we.Text, title);
// work around
if (we.Text.Contains("Seleni")) // I should be able to check on span title
{
var input = we.FindElement(By.CssSelector("input[id*='ctl00_cpm_AlbumMain_']"));
var id = input.GetAttribute("id");
Console.WriteLine(we.Text + " Id:" + id);
}
}

What you want to do is to use an xpath selector. Something like //*/span[title="SeleniumUpload"] if you want to match that exact title. If you want to just match something close to it, then you'll need to investigate using xpath with contains.

Related

How to write XPath for the text present outside the tags?

<div class="accordion_browse">
<a target="_blank" href="https://www.silverpages.sg/tools/e-care-locator"
aria-expanded="false" aria-controls="collapseExample">
<span class="browse_img">
<img src="/sites/assets/assets/directory/icons/eldercare.png"/>
</span>
Eldercare Services
<i class="indicator fa fa-chevron-right pull-right" aria-hidden="true"/>
</a>
</div>
Note: the text required is 'Eldercare Services'
Code:
string Eldercare = driver.FindElement(By.XPath("//a[contains(.,'Eldercare Services')")).Text;
Console.WriteLine(Eldercare + " found");
Assert.IsTrue(Eldercare.Contains("Elder"), Eldercare + " not found");
A text node is retrieved using the text node text()
XPath: "/body/div/text()"
That will retrieve all children text nodes of /body/div.
"/body/div/text()[1]" will retrieve the first text node inside /body/div
Additional Reading on XPath:
https://www.w3schools.com/xml/xpath_examples.asp
The given xpath is looking good, but By.XPath("//a[contains(.,'Eldercare Services')") example code missing a closing square bracket: By.XPath("//a[contains(.,'Eldercare Services')]")
With this fix, the xpath will give the a element
<a target="_blank" href="https://www.silverpages.sg/tools/e:care:locator" aria:expanded="false" aria:controls="collapseExample">
<span class="browse_img">
<img src="/sites/assets/assets/directory/icons/eldercare.png"/>
</span>
Eldercare Services longtext
<i class="indicator fa fa:chevron:right pull:right" aria:hidden="true"/>
</a>
The missing bracket should fix the code.
If Selenium would have work with text elements it could be
//a[contains(.,'Eldercare Services')]/text()
results would be a string array with 3 strings. the middle one has the expected string, because before the span and after i elements there will probably be two empty strings.
Text =
Text =
Eldercare Services longtext
Text =
If use a second filter for the text, it would result the expected string only
//a[contains(.,'Eldercare Services')]/text()[contains(.,'Elder')]

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;

XPath query not working(need find by text)

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')]

Parsing Radio button name only bringing up one value

I have written some code to parse the name from some radio buttons.
<div id="First" class="Size-Inputs">
<input rel="8" type="radio" value="13051374" name="idProduct-13051359"/> <span rel="L">L</span>
<input rel="8" type="radio" value="13051373" name="idProduct-13051359"/> <span rel="M">M</span>
<input rel="8" type="radio" value="13051372" name="idProduct-13051359"/> <span rel="S">S</span>
<input rel="8" type="radio" value="13051375"name="idProduct-13051359"/> <span rel="XL">XL</span> </div>
The problem which I am having when I try to parse the span rel to get the size names eg L,M,S,XL it is only coming up with the value L four times.
The code I am using is;
HtmlNodeCollection link = doc.DocumentNode.SelectNodes("//*[#id='First']/input");
if (link != null)
{
foreach (HtmlNode item in link)
{
string name = item.SelectSingleNode("//*[#id='First']/span").InnerText;
Console.WriteLine(name);
}
Console.ReadLine();
}
I was just wondering why the it is only picking up one value and printing it four times and how can you make it pick up the span rel for each of the variant input. Thanks for any help which you can provide
It seems that you don't need <input> element but <span> element. And <span> is not child of <input>. So assuming that it isn't typo or paste error, you can select <span> directly without selecting <input> element first :
HtmlNodeCollection link = doc.DocumentNode.SelectNodes("//*[#id='First']/span");
if (link != null)
{
foreach (HtmlNode item in link)
{
string name = item.InnerText;
Console.WriteLine(name);
}
Console.ReadLine();
}

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