Selecting drop down option using XPath & C# for an HTML table - c#

I want to choose a specific selection in this table, my table & specific selection being:
<table border="0" align="CENTER">
<tbody>
<tr>
<tr>
<td class="FieldLabel" valign="top" nowrap="" colspan="1">
<td valign="top" nowrap="" colspan="3">
<select class="HandleSelectChange" name="DISPLAY_RequestingProvider" style="width:100%;" size="1">
<option selected="" value="">Choose One</option>
<option value="1134303902NMDX0NMDX0 NMDX0Company Name">Company Name - 1234567890</option>
</select>
</td>
</tr>
</tr>
</tbody>
</table>
There are about 25 selections in this table, which is why I only included one of them.
Currently, my code can not find the element. My current code is as follows:
driver.FindElement(By.XPath("//tr[td[contains(text(),'Company Name')]]/td[2]")).Click();

Locate the select element and use SelectElement object to select an option by text:
IWebElement element = driver.FindElement(By.XPath("//tr[.//option = 'Company Name')]]//select[#name = 'DISPLAY_RequestingProvider']")).Click()
SelectElement selector = new SelectElement(element);
selector.SelectByText("Company Name");

Related

C# selenium find an element by text then click on checkbox

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();

Add tbody XML Element to table Element in XDcoument

Want to add <tbody> element in <table> elements if missing on Xdcoument.
<table class="newtable" id="item_559_Table1" cellpadding="0" cellspacing="0" data-its-style="width:11.4624em; border-spacing:0;">
<colgroup data-its-style="width:11.4624em; " />
<tr>
<td data-its-style="padding:0.2292em; vertical-align:top; ">
<p data-its-style="">My dad cooks up a pot of chicken soup, and</p>
</td>
</tr>
<tr>
<td data-its-style="padding:0.2292em; vertical-align:top; ">
<p data-its-style="font-weight:normal; ">This cold means I can’t taste a thing today!</p>
</td>
</tr>
</table>
Output should look like
<table class="newtable" id="item_559_Table1" cellpadding="0" cellspacing="0" data-its-style="width:11.4624em; border-spacing:0;">
<colgroup data-its-style="width:11.4624em; " />
<tbody>
<tr>
<td data-its-style="padding:0.2292em; vertical-align:top; ">
<p data-its-style="">My dad cooks up a pot of chicken soup, and</p>
</td>
</tr>
<tr>
<td data-its-style="padding:0.2292em; vertical-align:top; ">
<p data-its-style="font-weight:normal; ">This cold means I can’t taste a thing today!</p>
</td>
</tr>
</tbody>
</table>
**Not looking for XSLT solution.
One way to do it would be to grab the children of <table>, then add them back they way you want them.
var doc = XDocument.Load("file.xml");
var colgroup = doc.Root.Elements("colgroup");
var tr = doc.Root.Elements("tr");
// Add tr to tbody
var tbody = new XElement("tbody", tr);
// Replace the children of table with colgroup and tbody
doc.Root.ReplaceNodes(colgroup, tbody);

HtmlAgilityPack to get data from HTML table in C#

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"));

Failing to retrieve the third td nodes in an html list

I am trying to get the text "Very Good Country views" and "Good" using HTMLAgilityPack.
<div class="property-details-section">
<h5><span id="content_lblFurtherDetails">Further Details</span></h5>
<ul id="features">
<li style="display:block;">
<table border="0" cellpadding="0" cellspacing="0" width="500">
<tr>
<td style="width: 15px;">
<img src="../images/bullet.png" alt="bullet" />
</td>
<td style="width: 185px;">Views</td>
<td style="width: 300px;">Very Good Country views</td>
</tr>
</table>
</li>
</ul>
<li style="display:block;">
<table border="0" cellpadding="0" cellspacing="0" width="500">
<tr>
<td style="width: 15px;">
<img src="../images/bullet.png" alt="bullet" />
</td>
<td style="width: 185px;">Finish</td>
<td style="width: 300px;">Good</td>
<tr>
</table>
</li>
</div>
I have tried the following for "Very Good Country views" with no success:
HtmlNode text =
doc.DocumentNode.SelectSingleNode("//ul[#id='features']/li/table/tr/td[3]");
I am trying to get the text "Very Good Country views" and "Good"
You have to select 2 elements, so you should use SelectNodes instead of SelectSingleNode, if you want get the result at once.
var result = doc.DocumentNode.SelectNodes("//ul[#id='features']/li/*//td[last()]")
.Select(td => td.InnerText)
.ToList();
I think the problem about your XPath is that you should add brackets around the expression:
var text = doc.DocumentNode
.SelectSingleNode("(//ul[#id='features']/li/table/tr/td)[3]");
You can also try using LINQ:
var td = doc.Descendants("ul")
.First(x => x.GetAttributeValue("id","") == "features")
.Descendants("td")
.Skip(2)
.First();
var text = td.InnerText;

html agility how to process table in a hyperlink

I am working to get some information from a html table which has many rows like this. The given row is like one piece of info in a table cell. I need to get link, artist name, artist type from this table.
<a href="http://somesite/music/view_album.php?albumid=6468" style="color:#000;" sl-processed="1">
<table width="100%" border="0" bgcolor="#FFFFFF">
<tbody><tr>
<td colspan="2" align="left" valign="top" style="color:#900;">album title</td>
</tr>
<tr> <td width="31%" align="left" valign="top"> <img src="./albums_files/No_cover.png" width="90" height="80" border="0">
</td>
<td width="69%" align="left" valign="top">
<a class="leftcat" href="http://somelink/toartiset" sl-processed="1"> <strong>Rizwan-Muazzam</strong>
</a>
<br>
(<a class="leftcat" href="http://linktoartisttype/" sl-processed="1">
Some Artist Type </a>) <br>
<span class="leftcat">
Rated +: 0<br>
Rated -: 0 </span>
</td>
</tr>
<tr> <td valign="top" align="center" colspan="2">
</td> </tr>
</tbody></table>
</a>
I have done this
HtmlDocument doc = new HtmlDocument();
doc = new HtmlWeb().Load(albumUrl);
var nodes = doc.DocumentNode.SelectNodes("//a[#href]");
this gives me all the links which I need, now I want to get all the child information under the hyperlink.
Help will be appreciated.
Regards
Parminder
I would suggest using a loop to go through all the rows and then select the links and extract the info from them:
var rows = doc.DocumentNode.SelectNodes("//tr");
foreach (var row in rows)
{
var links = row.SelectNodes(".//a");
var artistLink = links[0].Attributes["href"];
var artistName = links[0].SelectSingleNode(".//strong/text()").InnerText;
var artistTypeLink = links[1].Attributes["href"];
var artistTypeName = links[1].SelectSingleNode(".//text()").InnerText;
// Store the results...
}

Categories