I want to search for example (UK) then click on checkbox that is in the same row
HTML (but tr can increase or decrease)
<tr class="ng-scope table-row-style">
<td class="ng-binding">US</td>
<td class="ng-binding">United States</td>
<td class="btn-td" style="padding: 0;">
<input type="checkbox" class="ng-pristine ng-untouched ng-valid ng-empty">
</td>
</tr>
<tr class="ng-scope table-row-style">
<td class="ng-binding">UK</td>
<td class="ng-binding">United Kingdom</td>
<td class="btn-td" style="padding: 0;">
<input type="checkbox" class="ng-pristine ng-untouched ng-valid ng-empty">
</td>
</tr>
<tr class="ng-scope table-row-style">
<td class="ng-binding">IN</td>
<td class="ng-binding">India</td>
<td class="btn-td" style="padding: 0;">
<input type="checkbox" class="ng-pristine ng-untouched ng-valid ng-empty">
</td>
</tr>
solved by
IList<IWebElement> tableRow = tableElement.FindElements(By.TagName("tr"));
IList<IWebElement> rowTD;
if (tableRow.Count > 0)
{
foreach (IWebElement row in tableRow)
{
rowTD = row.FindElements(By.TagName("td"));
if (rowTD[0].Text.Equals("UK"))
rowTD[2].Click();
}
}
You can find the parent tr element based on child td containing the desired text and then find the child input element as following:
//tr[.//td[contains(.,'UK')]]//input
The entire Selenium command finding and clicking this element could be
driver.FindElement(By.XPath("//tr[.//td[contains(.,'UK')]]//input")).Click();
Related
In a table there are multiple elements like this: <span class="currency"> 0</span>. The > 0< value is a formula value which changes each time. What would be the best way to locate the following element from the HTML:
<td class="alignLeft label">Initial</td>
<td class="alignRight readOnly">
<span class="currency"> 0</span>
</td>
<td></td>
Another element is located in this part of HTML:
<td class="alignLeft label">Initial</td>
<td class="alignRight readOnly">
<span class="currency"> 0</span>
</td>
<td></td>
<td class="alignRight readOnly">
<span class="currency"> 0</span>
</td>
<td></td>
Another one here:
<td class="alignLeft label">Initial</td>
<td class="alignRight readOnly">
<span class="currency"> 0</span>
</td>
<td></td>
<td class="alignRight readOnly">
<span class="currency"> 0</span>
</td>
<td></td>
<td class="alignRight readOnly">
<span class="currency"> 0</span>
</td>
Thanks.
You need to do something like this:
// this will return a list containing all elements with the currency class
var currencyElements = driver.FindElements(By.ClassName("currency"));
foreach (var element in currencyElements)
{
// access each individual element one by one
if (element.Text == "0")
// do something
}
This will return all elements with the currency class. If you want to be more specific, ie: you only want span elements with the currency class, you will need to use the OpenQA.Selenium.Support package and do a ByAll:
var byAll = new OpenQA.Selenium.Support.PageObjects.ByAll(new By[]
{
By.ClassName("currency"),
By.TagName("span")
});
Or instead just check element.TagName in the first example.
I am trying to get the td to display based on true/false values for Loan. Everything is solid until I hit this point, where it prints out both of the td's instead td corresponding to the value of true/false
<tbody>
<tr ng-repeat="dvd in dvds | filter: dvdFilter">
<td>{{dvd.Title}}</td>
<td>{{dvd.Rating.RatingName}}</td>
<td>{{dvd.MovieDirector}}</td>
<td>IMDb</td>
<td>
<button value={{dvd.DvdId}} class="btn btn-primary" id="btnDelete" ng-click="delete()">Delete</button>
</td>
<td ui-if{{dvd.Loan}}="true" style="color: green;">Available</td>
<td ui-if{{dvd.Loan}}="false" style="color: red;">Out on Loan</td>
</tr>
</tbody>
I believe your syntax is just a bit off. Try this on your td lines:
<td ng-if="dvd.Loan == 'true'" style="color: green;">Available</td>
<td ng-if="dvd.Loan == 'false'" style="color: red;">Out on Loan</td>
the answer ends up being
<td ng-if"dvd.Loan == true" style="color: red;">Out on Loan</td>
<td ng-if"dvd.Loan == false" style="color: green;">Available</td>
I am using HtmlAgilityPack to parse my html. My main goal is add some div elements around table row elements. However there are also some more elements in the table row. I only want to wrap it with div elements when the font's style is a background color of lightgreen.
Here's an example of what my html looks like:
<tr>
<td style="padding-left: 40pt;"><font style="background-color: lightgreen" color="black">Tove</font></td>
<td style="padding-left: 40pt;"><font style="background-color: lightgreen" color="black">To</font></td>
</tr>
however my current code:
HtmlNode[] nodes = document.DocumentNode.SelectNodes("//tr[//td[//font[#style='background-color: lightgreen']]]").ToArray();
foreach (HtmlNode node in nodes)
{
node.InnerHtml = node.InnerHtml.Replace( node.InnerHtml,"<div class=\"select-me\">" +node.InnerHtml + "</div>");
}
produces this html:
<tr>
<div class="select-me">
<td style="padding-left: 25pt;"><font style="background-color: white" color="black"><to</font><font style="background-color: white" color="black">></font></td>
<td style="padding-left: 25pt;"><font style="background-color: white" color="black"><to</font><font style="background-color: white" color="black">></font></td>
</div>
</tr>
<tr>
<div class="select-me">
<td style="padding-left: 40pt;"><font style="background-color: lightgreen" color="black">Tove</font></td>
<td style="padding-left: 40pt;"><font style="background-color: lightgreen" color="black">To</font></td>
</div>
</tr>
<tr>
<div class="select-me">
<td style="padding-left: 25pt;"><font style="background-color: white" color="black"></to></font></td>
<td style="padding-left: 25pt;"><font style="background-color: white" color="black"></to></font></td>
</div>
</tr>
the div elements don't surround the tr elements and every tr has a div element, when only a tr element that has font element of background color lightgreen should contain a div element. If you look at the first tr element it has 4 font elements, instead of two. Ideally, my goal is to insert div elements around a tr element when its font's element background is lightgreen. I have looked at other posts, but I am still having trouble.
Correct html should like:
<div class="select-me">
<tr>
<td style="padding-left: 25pt;"><font style="background-color: lightgreen" color="black">hello</font></td>
<td style="padding-left: 25pt;"><font style="background-color: lightgreen" color="black">goodbye</font></td>
</tr>
</div>
for (var i = 0; i < nodes.Length; i++ )
{
var node = nodes[i];
node = HtmlNode.CreateNode(node.OuterHtml.Replace(node.OuterHtml, "<div class=\"select-me\">" + node.OuterHtml + "</div>"));
}
Using HtmlAgilityPack, how to get data from input hidden values and tr, td rate in C# from HTML table?
I need to input hidden values to tr, td rate. How to get that information my below html table?
<table>
<caption>
<div id="cal_nav"
class="float_right">
<ul class="inline">
<li>
<a href="#"
onClick="changeRatesView('calendar')">Calendar View</a>
</li>
<li id="previous"
class="first">
<a title="September"
- "2015"
href="#"
onClick="searchPrevMonthAvailability()"> </a>
</li>
</div>
</caption>
<thead>
<tr>
<th>Date</th>
<th>Occupancy</th>
<th>Net Rate</th>
<th>Sell Rate</th>
</tr>
</thead>
<tbody>
<input type="hidden"
name="rateid"
value="234154166">
<tr>
<td>1</td>
<td>single</td>
<td>1652</td>
<td>2500</td>
</tr>
<tr>
<td>2</td>
<td>single</td>
<td>1454</td>
<td>4344</td>
</tr>
<input type="hidden"
name="rateid"
value="234154134">
<tr>
<td>1</td>
<td>single</td>
<td>1652</td>
<td>2500</td>
</tr>
<tr>
<td>2</td>
<td>single</td>
<td>1454</td>
<td>4344</td>
</tr>
<input type="hidden"
name="rateid"
value="234154145">
<tr>
<td>1</td>
<td>single</td>
<td>1652</td>
<td>2500</td>
</tr>
<tr>
<td>2</td>
<td>single</td>
<td>1454</td>
<td>4344</td>
</tr>
</tbody>
</table>
MY Linq code:
var tds= (from td in doc.DocumentNode.Descendants("table")
select td).ToList()[2].ChildNodes[2];
var trer = tdsyh.SelectNodes("//input[#type='hidden' and #name='rateid']|tr").Select(x => x).ToList();
You can try something like:
var hiddenFields = doc.DocumentNode.Descendants("input").Where(_ => _.GetAttributeValue("type", "").Equals("hidden") && _.Name.Equals("rateid"));
HtmlNode node = doc.DocumentNode.SelectNodes("//tr")[0];
foreach(HtmlTextNode n in node.SelectNodes("//text()"))
Console.WriteLine(n.Text);
HTML:
<table class="infobox" style="width: 17em; font-size: 100%;float: left;">
<tr>
<th style="text-align: center; background: #f08080;" colspan="3">خدیجہ مستور</th>
</tr>
<tr style="text-align: center;">
<td colspan="3"><img alt="خدیجہ مستور" src="//upload.wikimedia.org/wikipedia/ur/thumb/7/7b/Khatijamastoor.JPG/150px-Khatijamastoor.JPG" width="150" height="203" srcset="//upload.wikimedia.org/wikipedia/ur/thumb/7/7b/Khatijamastoor.JPG/225px-Khatijamastoor.JPG 1.5x, //upload.wikimedia.org/wikipedia/ur/thumb/7/7b/Khatijamastoor.JPG/300px-Khatijamastoor.JPG 2x"><br>
<div style="font-size: 90%">خدیجہ مستور</div>
</td>
</tr>
<tr>
<th style="background: #f08080;" colspan="3">ادیب</th>
</tr>
<tr>
<td><b>ولادت</b></td>
<td colspan="2">1930ء، لکھنؤ، برطانوی ہندوستان</td>
</tr>
<tr>
<td><b>اصناف ادب</b></td>
<td colspan="2">ناول</td>
</tr>
<tr>
<td><b>معروف تصانیف</b></td>
<td colspan="2">آنگن</td>
</tr>
</table>
Output Should be :
خدیجہ مستور
but i found :
خدیجہ مستور
خدیجہ مستور
ادیب
ولادت
1930ء
،
لکھنؤ
،
برطانوی ہندوستان
اصناف ادب
ناول
معروف تصانیف
آنگن
Why node.selectNodes("//text()") is selecting all text() nodes in document rather text() nodes from just first tr tag??
Because you are adding two forward slashes to the beginning of your XPath (//tr), which selects all of the elements in the document, not just descendants of the selected node.
Try this instead:
foreach (HtmlTextNode n in node.SelectNodes("text()"))
Or just simplify the XPath to:
var node = doc.DocumentNode.SelectSingleNode("//tr[1]/text()");
Console.WriteLine(node.Text);