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.
Related
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();
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>
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);
I have the following code/markup
#for (int m = 0; m < Model.Parts.Count; m++ ) {
var item2 = Model.Parts[m];
<tr id='#item2.WorkOrderPartId'>
<td></td>
<td style='text-align:center'> #item2.LineNo </td>
<td> #item2.SalesOrderLineNo</td>
<td style='text-align:center'>
#item2.Length
</td>
<td style='width:100px;text-align:right'>
</td>
<td style='width:100px;text-align:left'>
</td>
</td>
<td style='padding-right:10px'>0</td>
<td style='padding-right:10px'>0</td>
<td class='RemainingWeight'></td>
<td> </td>
</tr>
}
When this view get executed following error is getting OnException.
Encountered end tag "tr" with no matching start tag. Are your
start/end tags properly balanced?
Please helpme? :(
Good indentation is the key to success.
#for (int m = 0; m < Model.Parts.Count; m++ ) {
var item2 = Model.Parts[m];
<tr id='#item2.WorkOrderPartId'>
<td></td>
<td style='text-align:center'> #item2.LineNo </td>
<td> #item2.SalesOrderLineNo</td>
<td style='text-align:center'>
#item2.Length
</td>
<td style='width:100px;text-align:right'></td>
<td style='width:100px;text-align:left'></td>
<td style='padding-right:10px'>0</td>
<td style='padding-right:10px'>0</td>
<td class='RemainingWeight'></td>
<td></td>
</tr>
}
You had
<td style='width:100px;text-align:left'>
</td>
</td>
Which should just be
<td style='width:100px;text-align:left'></td>
There is an additional end tag without a start tag for the same in the code you have shared. The one marked in ** below from your code snippet.
<td style='width:100px;text-align:left'>
</td>
**</td>**
In case of this code block only You have one extra td end tag that is:
<td style='width:100px;text-align:left'>
</td>
</td>
... Athough to detect in future If you are using visual studio then click on the starting tag of a it will highlight the corresponding end Tag. If it doesn't highlight an end tag for one of the starting tag then that one is missing the end tag.
I am not sure is this solve your problem.
Anyway you can just try this code.
#for (int m = 0; m < Model.Parts.Count; m++)
{
var item2 = Model.Parts[m];
<tr id='#item2.WorkOrderPartId'>
<td></td>
<td style='text-align:center'> #item2.LineNo </td>
<td> #item2.SalesOrderLineNo</td>
<td style='text-align:center'>
#item2.Length
</td>
<td style='width:100px;text-align:right'></td>
<td style='width:100px;text-align:left'></td>
<td style='padding-right:10px'>0</td>
<td style='padding-right:10px'>0</td>
<td class='RemainingWeight'></td>
<td> </td>
</tr>
}
There was an extra closing td tag in your code.
How to read <table> into onmouseover event with C# and HTMLAgilityPack?
markup code :
<a href="#" class="chan_live_not_free" onclick="return false;" onmouseover="return overlib('
<table>
<tr class=fieldRow>
<td class=posH_col width=40>
<strong>pos</strong>
</td>
<td class=rest_col width=90>
<strong>satellite</strong>
</td>
<td class=freqH_col width=50>
<strong>freq</strong>
</td>
<td class=rest_col width=90>
<strong>symbol</strong>
</td>
<td class=rest_col width=90>
<strong>encryption</strong>
</td>
</tr>
<tr>
<td class="pos_col">39.0°e</td>
<td class=rest_col>Hellas Sat 2</td>
<td class="freq_col">12.606 H</td>
<td class=rest_col>30000 - 2/3</td>
<td class=enc_not_live>MPEG-4 BulCrypt</td>
</tr>
</table>',CAPTION, 'Arena Sport 4 (serbia) – 19/10/14 - 11:30');" onmouseout="return nd();">
Arena Sport 4 (serbia)
</a>
I need to read the table into onmouseover event. How does it read?
You could get the element attribute of the <a> tag with HTML Agility Pack and then using regular expressions get the <table> inside the string, something like the following code :
var html = #"<a href='#' class='chan_live_not_free' onclick='return false;' onmouseover='return overlib(
<table>
<tr class=fieldRow>
<td class=posH_col width=40>
<strong>pos</strong>
</td>
<td class=rest_col width=90>
<strong>satellite</strong>
.
.
.
<tr>
<td class="pos_col">39.0°e</td>
<td class=rest_col>Hellas Sat 2</td>
<td class="freq_col">12.606 H</td>
<td class=rest_col>30000 - 2/3</td>
<td class=enc_not_live>MPEG-4 BulCrypt</td>
</tr>
</table>,CAPTION, 'Arena Sport 4 (serbia) – 19/10/14 - 11:30');' onmouseout='return nd();'>
Arena Sport 4 (serbia)
</a>";
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var value = doc.DocumentNode.SelectSingleNode("//a[#class='chan_live_not_free']").Attributes["onmouseover"].Value;
var text = Regex.Matches(value, #"<table>([^)]*)</table>")[0].Value;