Parsing HTML content with C# Parser - c#

I have the following HTML file, I want to get each H2 (Standard (Flexible Rate).. and Executive (Flexible Rate) ... with the Room Only , Breakfast Included .
Then push the Room Only and Breakfast Include with 2 Prices of each into objects where I have Standard along with 2 prices from Room Only and Breakfast Included, and the same for Executive
I tried Fizzler with AgilityPack however, I couldn't get the correct results, could you please suggest me an idea or one good parser for this case? Thanks
<div id="accordionResizer" style="padding:5px; height:300px; border-radius:6px;" class="ui-widget-content regestancias">
<div id="accordion" class="dias">
<h2>
<a href="#">
Standard (Flexible Rate) from 139 €
</a>
</h2>
<div class="estancias_precios estancias_precios_new">
<table style="width: 285px;">
<tr class="" title="">
<cont>
<td style="width: 25px;">
<input type="radio" name="estancias" id="tarifa602385" elem="tarifa" idelem="602" idreg="385" precio="139" reg="Only%20Bed" nombre="Standard%20%28Flexible%20Rate%29" />
</td>
<td style="width: 155px;">
<label class="descrip" for="tarifa602385" precio="139.00" reg="Only%20Bed" nombre="Standard%20%28Flexible%20Rate%29">
Only Bed
</label>
</td>
<td style="width: 55px;"></td>
<td style="width: 55px;">
<strong class="precios_mos">139.00 €</strong>
</td>
</cont>
</tr>
<tr class="" title="">
<cont>
<td style="width: 25px;">
<input type="radio" name="estancias" id="tarifa602386" elem="tarifa" idelem="602" idreg="386" precio="156.9" reg="Breakfast%20Included" nombre="Standard%20%28Flexible%20Rate%29" />
</td>
<td style="width: 155px;">
<label class="descrip" for="tarifa602386" precio="156.90" reg="Breakfast%20Included" nombre="Standard%20%28Flexible%20Rate%29">
Breakfast Included
</label>
</td>
<td style="width: 55px;"></td>
<td style="width: 55px;">
<strong class="precios_mos">156.90 €</strong>
</td>
</cont>
</tr>
</table>
</div>
<h2>
<a href="#">
Executive (Flexible Rate) from 169 €
</a>
</h2>
<div class="estancias_precios estancias_precios_new">
<table style="width: 285px;">
<tr class="" title="">
<cont>
<td style="width: 25px;">
<input type="radio" name="estancias" id="tarifa666385" elem="tarifa" idelem="666" idreg="385" precio="169" reg="Only%20Bed" nombre="Executive%20%28Flexible%20Rate%29" />
</td>
<td style="width: 155px;">
<label class="descrip" for="tarifa666385" precio="169.00" reg="Only%20Bed" nombre="Executive%20%28Flexible%20Rate%29">
Only Bed
</label>
</td>
<td style="width: 55px;"></td>
<td style="width: 55px;">
<strong class="precios_mos">169.00 €</strong>
</td>
</cont>
</tr>
<tr class="" title="">
<cont>
<td style="width: 25px;">
<input type="radio" name="estancias" id="tarifa666386" elem="tarifa" idelem="666" idreg="386" precio="186.9" reg="Breakfast%20Included" nombre="Executive%20%28Flexible%20Rate%29" />
</td>
<td style="width: 155px;">
<label class="descrip" for="tarifa666386" precio="186.90" reg="Breakfast%20Included" nombre="Executive%20%28Flexible%20Rate%29">
Breakfast Included
</label>
</td>
<td style="width: 55px;"></td>
<td style="width: 55px;">
<strong class="precios_mos">186.90 €</strong>
</td>
</cont>
</tr>
</table>
</div>
</div>
</div>

Here you go with a quick and dirty approach:
class RoomInfo
{
public String Name { get; set; }
public Dictionary<String, Double> Prices { get; set; }
}
private static void HtmlFile()
{
List<RoomInfo> rooms = new List<RoomInfo>();
HtmlDocument document = new HtmlDocument();
document.Load("file.txt");
var h2Nodes = document.DocumentNode.SelectNodes("//h2");
foreach (var h2Node in h2Nodes)
{
RoomInfo roomInfo = new RoomInfo
{
Name = h2Node.InnerText.Trim(),
Prices = new Dictionary<string, double>()
};
var labels = h2Node.NextSibling.NextSibling.SelectNodes(".//label");
foreach (var label in labels)
{
roomInfo.Prices.Add(label.InnerText.Trim(), Convert.ToDouble(label.Attributes["precio"].Value, CultureInfo.InvariantCulture));
}
rooms.Add(roomInfo);
}
}
The rest is up to you! ;-)

Related

Unable to locate element inside a table using xpath with selenium

I want to click on a button inside my table each row has a update button I want to click on a specfic button inside my table.
Here is a what my table looks like:
<table _ngcontent-vhp-c82="" datatable="" id="dtOptionsComments" class="display table table-striped table-bordered dt-responsive dataTable dtr-inline" aria-describedby="dtOptionsComments_info" style="width: 100%;" width="100%">
<thead _ngcontent-vhp-c82="">
<tr _ngcontent-vhp-c82="">
<th _ngcontent-vhp-c82="" class="no-marking sorting_disabled" rowspan="1" colspan="1" style="width: 50.4px;" aria-label=""></th>
<th _ngcontent-vhp-c82="" class="sorting sorting_asc" tabindex="0" aria-controls="dtOptionsComments" rowspan="1" colspan="1" style="width: 1109.4px;" aria-label="Comment.Comment Shipping.ShippingDatatable.aria.sortDescending" aria-sort="ascending">Comentario Shipping.Shipping</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td class="no-marking dtr-control">
<a href="javascript:void(0);">
<span data-toggle="modal" data-target="#update-modal" update-comment-text="6 MESES DE GARANTIA" update-comment-id="5" class="material-icons md-18 clickable"> edit </span>
</a>
<a href="javascript:void(0);">
<span data-toggle="modal" data-target="#delete-modal" delete-comment-id="5" class="material-icons clickable">delete</span>
</a>
</td>
<td class="sorting_1">6 MESES DE GARANTIA</td>
</tr>
<!-- MORE ROWS!!! -->
</tbody>
<tfoot _ngcontent-vhp-c82="">
<tr _ngcontent-vhp-c82="">
<td _ngcontent-vhp-c82="" class="no-marking" rowspan="1" colspan="1">
<a _ngcontent-vhp-c82="" href="javascript:void(0);">
<span _ngcontent-vhp-c82="" class="material-icons clickable"> add_box </span>
</a>
</td>
<td _ngcontent-vhp-c82="" rowspan="1" colspan="1">
<input _ngcontent-vhp-c82="" formcontrolname="addComment" type="text" id="addComment" name="addComment" class="form-control ng-untouched ng-pristine ng-invalid">
<!---->
</td>
</tr>
</tfoot>
</table>
Here is my code trials:
IWebElement btnUpdate = _driver.FindElement(By.XPath("//*[update-comment-id='" + commentAction.GetLastQuoteInsertId().ToString() + "']"));
btnUpdate.Click();
I have validated that the function GetLastQuoteInsertId returns the proper value
Why is my xPath selector wrong how can I fix it thank you for your help.
You were almost there. While considering a xpath the attribute_name should be always preceded by a # sign.
Additionally to make the xpath more canonical as the element is a <span> element you can mention //span to start the xpath.
Effectively, your line of code will be:
IWebElement btnUpdate = _driver.FindElement(By.XPath("//span[#update-comment-id='" + commentAction.GetLastQuoteInsertId().ToString() + "']"));
btnUpdate.Click();

exclude certain element in xPath c#

My html structure like this:
<table id = "searchResultsTable">
<tbody class="searchResultsRowClass">
<tr>
<td>....</td>
<td>....</td>
</tr>
<tr>
<td>....</td>
<td>....</td>
</tr>
<!--it repeats 21 times in every page -->
</tbody>
</table>
My C# code:
private void button1_Click(object sender, EventArgs e)
{
var url = "url";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
string sourceCode = sr.ReadToEnd();
HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
document.LoadHtml(sourceCode);
var rows = document.DocumentNode.SelectNodes("//*[#id='searchResultsTable']/tbody/tr");
foreach (var row in rows)
{
if (row.ChildNodes.Count > 0)
{
var name = row.SelectSingleNode("td[2]/a[1]").InnerText;
var year = row.SelectSingleNode("td[3]").InnerText;
var km = row.SelectSingleNode("td[4]").InnerText;
var color = row.SelectSingleNode("td[5]").InnerText;
var price = row.SelectSingleNode("td[6]").InnerText;
var date = row.SelectSingleNode("td[7]").InnerText;
var location = row.SelectSingleNode("td[8]").InnerText;
Console.WriteLine("name" + name + "\nyear" + year + "\nkm" + km + "\ncolor" + color + "\nprice" + price + "\ndate" + date + "\nlocation" + location);
}
}
In my html,tr[5] is empty. Therefore, I have to exclude it.
I tried /root/*[not(self::a)] like /tr/*[not(self::tr[5])]. But it didn't work.
Now, I can get only first 4 tr elements. Then,
NullReferenceException occurred.
How can I exclude one of tr element in xpath?
<table id="searchResultsTable" class="">
<thead>
<tr>
<td class="searchResultsFirstColumn"> </td>
<td class="">İlan Başlığı</td>
<td>
<a href="/otomobil?sorting=a5_asc&price_min=40000&price_max=40000">
Yıl</a>
</td>
<td>
<a href="/otomobil?sorting=a4_asc&price_min=40000&price_max=40000">
Km</a>
</td>
<td>Renk</td>
<td class="searchResultsPriceHeader">
<a href="/otomobil?sorting=price_asc&price_min=40000&price_max=40000">
Fiyat</a>
</td>
<td class="searchResultsDateHeader">
<a href="/otomobil?sorting=date_desc&price_min=40000&price_max=40000">
İlan Tarihi</a>
</td>
<td class="searchResultsLastColumn searchResultsLocationHeader">
<a href="/otomobil?sorting=address_desc&price_min=40000&price_max=40000">
İl / İlçe</a>
</td>
<td class="searchResultsIgnoredColumn"></td>
</tr>
</thead>
<tbody class="searchResultsRowClass">
<tr data-id="464336919" class="searchResultsItem ">
<td class="searchResultsLargeThumbnail">
<a href="/ilan/vasita-otomobil-fiat-automobilworld-den-2015-linea-pop-89.000km-degisensiz-faturali-464336919/detay">
<img src="https://image5.sahibinden.com/photos/33/69/19/thmb_464336919waj.jpg" alt="AUTOMOBİLWORLD'DEN 2015 LINEA POP 89.000KM DEĞİŞENSİZ FATURALI #464336919" title="AUTOMOBİLWORLD'DEN 2015 LINEA POP 89.000KM DEĞİŞENSİZ FATURALI">
</a></td>
<td class="searchResultsTitleValue ">
<input id="favoriteClassifiedsVisibility" type="hidden" value="true">
<div class="action-wrapper" data-classified-id="464336919">
<div class="add-to-favorites last favorite">
<a href="#" class="action classifiedAddFavorite trackClick trackId_favorite hidden">
Favorilerime Ekle</a>
<a href="#" class="action classifiedRemoveFavorite trackClick trackId_favorite disable">
Favorilerimde</a>
</div>
<div class="compare hidden">
<a class="facetedCheckbox action compare-classified">
<i></i>Karşılaştır</a>
</div>
</div>
<a class="classifiedTitle" href="/ilan/vasita-otomobil-fiat-automobilworld-den-2015-linea-pop-89.000km-degisensiz-faturali-464336919/detay">
AUTOMOBİLWORLD'DEN 2015 LINEA POP 89.000KM DEĞİŞENSİZ FATURALI</a>
<a class="titleIcon store-icon" href="https://automobilworld.sahibinden.com/" title="AUTOMOBIL WORLD" style="visibility: visible;">
<img class="titleIcon" src="https://s0.shbdn.com/assets/images/iconStore:e98c183976843a1e5b3d4e580d614009.png" alt="AUTOMOBIL WORLD" title="AUTOMOBIL WORLD" style="visibility: visible;">
</a>
<img class="titleIcon" alt="Haritalı İlan" title="Haritalı İlan" src="https://s0.shbdn.com/assets/images/iconHasMap:1f5f8f9b79e391584fe00304345baa05.png" style="visibility: visible;">
<br>
<div class="classifiedSubtitle " style="visibility: visible;">
Fiat > Linea > 1.3 Multijet Pop</div>
</td>
<td class="searchResultsAttributeValue">
2015</td>
<td class="searchResultsAttributeValue">
89.000</td>
<td class="searchResultsAttributeValue">
Beyaz</td>
<td class="searchResultsPriceValue">
<div> 40.000 TL</div></td>
<td class="searchResultsDateValue">
<span>21 Temmuz</span>
<br>
<span>2017</span>
</td>
<td class="searchResultsLocationValue">
İstanbul<br>Büyükçekmece</td>
<td class="ignore-me">
<a href="#" class="mark-as-not-ignored disable">
Göster</a>
</td>
</tr>
<tr data-id="460187522" class="searchResultsItem ">
<td class="searchResultsLargeThumbnail">
<a href="/ilan/vasita-otomobil-volkswagen-orjinal-kilometre-124800km-lpg-460187522/detay">
<img src="https://image5.sahibinden.com/photos/18/75/22/thmb_460187522s6x.jpg" alt="ORJİNAL KİLOMETRE 124800KM LPG #460187522" title="ORJİNAL KİLOMETRE 124800KM LPG">
</a></td>
<td class="searchResultsTitleValue ">
<input id="favoriteClassifiedsVisibility" type="hidden" value="true">
<div class="action-wrapper" data-classified-id="460187522">
<div class="add-to-favorites last favorite">
<a href="#" class="action classifiedAddFavorite trackClick trackId_favorite hidden">
Favorilerime Ekle</a>
<a href="#" class="action classifiedRemoveFavorite trackClick trackId_favorite disable">
Favorilerimde</a>
</div>
<div class="compare hidden">
<a class="facetedCheckbox action compare-classified">
<i></i>Karşılaştır</a>
</div>
</div>
<a class="classifiedTitle" href="/ilan/vasita-otomobil-volkswagen-orjinal-kilometre-124800km-lpg-460187522/detay">
ORJİNAL KİLOMETRE 124800KM LPG</a>
<a class="titleIcon store-icon" href="https://42.sahibinden.com/" title="HÜSEYİN ÖRNEK" style="visibility: visible;">
<img class="titleIcon" src="https://s0.shbdn.com/assets/images/iconStore:e98c183976843a1e5b3d4e580d614009.png" alt="HÜSEYİN ÖRNEK" title="HÜSEYİN ÖRNEK" style="visibility: visible;">
</a>
<img class="titleIcon" alt="Haritalı İlan" title="Haritalı İlan" src="https://s0.shbdn.com/assets/images/iconHasMap:1f5f8f9b79e391584fe00304345baa05.png" style="visibility: visible;">
<br>
<div class="classifiedSubtitle " style="visibility: visible;">
Volkswagen > Passat > 1.6 Comfortline</div>
</td>
<td class="searchResultsAttributeValue">
2002</td>
<td class="searchResultsAttributeValue">
124.800</td>
<td class="searchResultsAttributeValue">
Beyaz</td>
<td class="searchResultsPriceValue">
<div> 40.000 TL</div></td>
<td class="searchResultsDateValue">
<span>10 Temmuz</span>
<br>
<span>2017</span>
</td>
<td class="searchResultsLocationValue">
Konya<br>Selçuklu</td>
<td class="ignore-me">
<a href="#" class="mark-as-not-ignored disable">
Göster</a>
</td>
</tr>
<tr data-id="397435322" class="searchResultsItem ">
<td class="searchResultsLargeThumbnail">
<a href="/ilan/vasita-otomobil-renault-2013-renault-clio-4-touch-paket-boyasizzzzzzzzzzzzzzzzzz-397435322/detay">
<img src="https://image5.sahibinden.com/photos/43/53/22/thmb_397435322ao8.jpg" alt="2013 RENAULT CLİO 4 TOUCH PAKET ___BOYASIZZZZZZZZZZZZZZZZZZ___ #397435322" title="2013 RENAULT CLİO 4 TOUCH PAKET ___BOYASIZZZZZZZZZZZZZZZZZZ___">
</a></td>
<td class="searchResultsTitleValue ">
<input id="favoriteClassifiedsVisibility" type="hidden" value="true">
<div class="action-wrapper" data-classified-id="397435322">
<div class="add-to-favorites last favorite">
<a href="#" class="action classifiedAddFavorite trackClick trackId_favorite hidden">
Favorilerime Ekle</a>
<a href="#" class="action classifiedRemoveFavorite trackClick trackId_favorite disable">
Favorilerimde</a>
</div>
<div class="compare hidden">
<a class="facetedCheckbox action compare-classified">
<i></i>Karşılaştır</a>
</div>
</div>
<a class="classifiedTitle" href="/ilan/vasita-otomobil-renault-2013-renault-clio-4-touch-paket-boyasizzzzzzzzzzzzzzzzzz-397435322/detay">
2013 RENAULT CLİO 4 TOUCH PAKET ___BOYASIZZZZZZ­ZZZZZZZZZZZZ___</a>
<a class="titleIcon store-icon" href="https://guvenototarsus.sahibinden.com/" title="GÜVEN OTOMOTİV" style="visibility: visible;">
<img class="titleIcon" src="https://s0.shbdn.com/assets/images/iconStore:e98c183976843a1e5b3d4e580d614009.png" alt="GÜVEN OTOMOTİV" title="GÜVEN OTOMOTİV" style="visibility: visible;">
</a>
<img class="titleIcon" alt="Haritalı İlan" title="Haritalı İlan" src="https://s0.shbdn.com/assets/images/iconHasMap:1f5f8f9b79e391584fe00304345baa05.png" style="visibility: visible;">
<br>
<div class="classifiedSubtitle " style="visibility: visible;">
Renault > Clio > 1.2 Touch</div>
</td>
<td class="searchResultsAttributeValue">
2013</td>
<td class="searchResultsAttributeValue">
74.000</td>
<td class="searchResultsAttributeValue">
Siyah</td>
<td class="searchResultsPriceValue">
<div> 40.000 TL</div></td>
<td class="searchResultsDateValue">
<span>08 Temmuz</span>
<br>
<span>2017</span>
</td>
<td class="searchResultsLocationValue">
Mersin<br>Tarsus</td>
<td class="ignore-me">
<a href="#" class="mark-as-not-ignored disable">
Göster</a>
</td>
</tr>
<tr data-id="458875511" class="searchResultsItem ">
<td class="searchResultsLargeThumbnail">
<a href="/ilan/vasita-otomobil-ford-2011-model-40.binde-otomatik-458875511/detay">
<img src="https://image5.sahibinden.com/photos/87/55/11/thmb_458875511431.jpg" alt="2011 MODEL 40.binde OTOMATİK #458875511" title="2011 MODEL 40.binde OTOMATİK">
</a></td>
<td class="searchResultsTitleValue ">
<input id="favoriteClassifiedsVisibility" type="hidden" value="true">
<div class="action-wrapper" data-classified-id="458875511">
<div class="add-to-favorites last favorite">
<a href="#" class="action classifiedAddFavorite trackClick trackId_favorite hidden">
Favorilerime Ekle</a>
<a href="#" class="action classifiedRemoveFavorite trackClick trackId_favorite disable">
Favorilerimde</a>
</div>
<div class="compare hidden">
<a class="facetedCheckbox action compare-classified">
<i></i>Karşılaştır</a>
</div>
</div>
<a class="classifiedTitle" href="/ilan/vasita-otomobil-ford-2011-model-40.binde-otomatik-458875511/detay">
2011 MODEL 40.binde OTOMATİK</a>
<a class="titleIcon store-icon" href="https://mackaotomotiv.sahibinden.com/" title="MAÇKA OTOMOTİV" style="visibility: visible;">
<img class="titleIcon" src="https://s0.shbdn.com/assets/images/iconStore:e98c183976843a1e5b3d4e580d614009.png" alt="MAÇKA OTOMOTİV" title="MAÇKA OTOMOTİV" style="visibility: visible;">
</a>
<img class="titleIcon" alt="Haritalı İlan" title="Haritalı İlan" src="https://s0.shbdn.com/assets/images/iconHasMap:1f5f8f9b79e391584fe00304345baa05.png" style="visibility: visible;">
<br>
<div class="classifiedSubtitle " style="visibility: visible;">
Ford > Fiesta > 1.4 Titanium</div>
</td>
<td class="searchResultsAttributeValue">
2011</td>
<td class="searchResultsAttributeValue">
40.000</td>
<td class="searchResultsAttributeValue">
Gümüş Gri</td>
<td class="searchResultsPriceValue">
<div> 40.000 TL</div></td>
<td class="searchResultsDateValue">
<span>07 Temmuz</span>
<br>
<span>2017</span>
</td>
<td class="searchResultsLocationValue">
Düzce<br>Merkez</td>
<td class="ignore-me">
<a href="#" class="mark-as-not-ignored disable">
Göster</a>
</td>
</tr>
<tr class="searchResultsPromoToplist">
<td colspan="12">
<div><strong>Siz de ilanınızın yukarıda yer almasını istiyorsanız <u>tıklayın</u>.</strong></div>
</td>
</tr>
<tr data-id="465252780" class="searchResultsItem ">
<td class="searchResultsLargeThumbnail">
<a href="/ilan/vasita-otomobil-peugeot-multimedia-sistemli-temiz-2014-active-paket-301-465252780/detay">
<img class="searchResultThumbnailPlaceholder otherNoImage" src="https://s0.shbdn.com/assets/images/iconHasMegaPhoto:1d086aab554fd92d49d3762a0542888a.png" alt="Multimedia Sistemli Temiz 2014 Active Paket 301 #465252780" title="Megafotolu ilan">
</a></td>
<td class="searchResultsTitleValue ">
<input id="favoriteClassifiedsVisibility" type="hidden" value="true">
<div class="action-wrapper" data-classified-id="465252780">
<div class="add-to-favorites last favorite">
<a href="#" class="action classifiedAddFavorite trackClick trackId_favorite hidden">
Favorilerime Ekle</a>
<a href="#" class="action classifiedRemoveFavorite trackClick trackId_favorite disable">
Favorilerimde</a>
</div>
<div class="compare hidden">
<a class="facetedCheckbox action compare-classified">
<i></i>Karşılaştır</a>
</div>
</div>
<img class="titleIcon" src="https://s0.shbdn.com/assets/images/iconNew:c9b443de96056beb84b4cdc03ca5051c.png" alt="Yeni İlan" title="Yeni İlan" style="visibility: visible;">
<a class="classifiedTitle" href="/ilan/vasita-otomobil-peugeot-multimedia-sistemli-temiz-2014-active-paket-301-465252780/detay">
Multimedia Sistemli Temiz 2014 Active Paket 301</a>
<img class="titleIcon" alt="Haritalı İlan" title="Haritalı İlan" src="https://s0.shbdn.com/assets/images/iconHasMap:1f5f8f9b79e391584fe00304345baa05.png" style="visibility: visible;">
<br>
<div class="classifiedSubtitle " style="visibility: visible;">
Peugeot > 301 > 1.6 HDi Active</div>
</td>
<td class="searchResultsAttributeValue">
2014</td>
<td class="searchResultsAttributeValue">
95.500</td>
<td class="searchResultsAttributeValue">
Beyaz</td>
<td class="searchResultsPriceValue">
<div> 40.000 TL</div></td>
<td class="searchResultsDateValue">
<span>25 Temmuz</span>
<br>
<span>2017</span>
</td>
<td class="searchResultsLocationValue">
İstanbul<br>Kadıköy</td>
<td class="ignore-me">
<a href="#" class="mark-as-not-ignored disable">
Göster</a>
</td>
</tr>
Here is part of the html. I tried to exclude <tr class="searchResultsPromoToplist">
If you only want to match <tr> elements that have <td> child elements, change you XPath from:
//*[#id='searchResultsTable']/tbody/tr
to:
//*[#id='searchResultsTable']/tbody/tr[td]
EDIT
From the HTML you posted it would seem that the <tr> elements you are interested in all have a data-id attribute. If that is the case, change your XPath to:
//*[#id='searchResultsTable']/tbody/tr[#data-id]
Try
string.isnullorempty(yourrowelement)? yourrowelement:string.empty

Handling tables in c# selenium

<table cellpadding="0" cellspacing="0" onclick="" style="width: 1345px;">
<tbody>
<tr id="item_tcm:222-382904-131104" title="2. Publish to WIP (tcm:222-382904-131104)" class="item even" c:drawn="true">
<td class="col0 icon odd" value="T131104L0P0">
<div class="icon" style="background-image: url("/WebUI/Editors/CME/Themes/Carbon2/icon_v7.1.0.66.55_.png?name=T131104L0P0&size=16");"></div>
</td>
<td class="col1 even">
<div class="text">2. Publish to WIP</div>
</td>
<td class="col2 odd">
<div class="text">JH Anchor link 2</div>
</td>
<td class="col3 even">
<div class="text">S070 Public Site US English</div>
</td>
<td class="col4 odd" value="2015-12-23T14:41:04">
<div class="text">12/23/2015 2:41 PM</div>
</td>
<td class="col5 even">
<div class="text">NT AUTHORITY\SYSTEM</div>
</td>
<td class="col6 odd" value="">
<div class="text">
<span style="color: #f00"></span>
</div>
</td>
<td class="col7 even" value="16">
<div class="text">Suspended</div>
</td>
<td class="col8 odd">
<div class="text">NT AUTHORITY\SYSTEM</div>
</td>
<td class="col9 even">
<div class="text">Publishing Failed</div>
</td>
</tr>
<tr id="item_tcm:222-382901-131104" title="2. Publish to WIP (tcm:222-382901-131104)" class="item even" c:drawn="true">
<td class="col0 icon odd" value="T131104L0P0">
<div class="icon" style="background-image: url("/WebUI/Editors/CME/Themes/Carbon2/icon_v7.1.0.66.55_.png?name=T131104L0P0&size=16");"></div>
</td>
<td class="col1 even">
<div class="text">2. Publish to WIP</div>
</td>
<td class="col2 odd">
<div class="text">JH_anchor link</div>
</td>
<td class="col3 even">
<div class="text">S070 Public Site US English</div>
</td>
<td class="col4 odd" value="2015-12-23T14:17:51">
<div class="text">12/23/2015 2:17 PM</div>
</td>
<td class="col5 even">
<div class="text">NT AUTHORITY\SYSTEM</div>
</td>
<td class="col6 odd" value="">
<div class="text">
<span style="color: #f00"></span>
</div>
</td>
<td class="col7 even" value="16">
<div class="text">Suspended</div>
</td>
<td class="col8 odd">
<div class="text">NT AUTHORITY\SYSTEM</div>
</td>
<td class="col9 even">
<div class="text">Publishing Failed</div>
</td>
</tr>
.....
</tbody>
</table>
I have collection of rows. Inside each row i have 10 columns(td). I want to iterate to each row. For each row I want to get the 8th and 10 th column.
Note :- The test case will get Fail if the 8th column value is "Suspended" and 10th column value is "Publishing Failed" or else the test case would get Pass
I tried the below logic
IWebElement tableElement = driver.FindElement(By.XPath("/html/body/table"));
IList<IWebElement> tableRow = tableElement.FindElements(By.TagName("tr"));
foreach (var item in tableRow)
{
}
I'm not sure how to proceed further. Could anyone help me? Thanks in advance
Your logic is good:
IWebElement tableElement = driver.FindElement(By.XPath("/html/body/table"));
IList<IWebElement> tableRow = tableElement.FindElements(By.TagName("tr"));
IList<IWebElement> rowTD;
foreach (IWebElement row in tableRow)
{
rowTD = row.FindElements(By.TagName("td"));
if(rowTD.Count > 9)
{
if(rowTD[8].Text.Equals("Suspended") && rowTD[10].Text.Equals("Publishing Failed");
//test failed
}
}
What if you would just try to find the rows having the 8th column value "Suspended" and 10th column value "Publishing Failed":
IList<IWebElement> rows = tableElement.FindElements(By.TagName("//table//tr[td[8]/div = 'Suspended' and td[10]/div = 'Publishing Failed']"));
Then, you can fail the test if rows list is not empty.
Try this:
foreach (var item in tableRow)
{
IWebElement column7 = item.FindElement(By.CssSelector("[class*='col7']"));
IWebElement column9 = item.FindElement(By.CssSelector("[class*='col9']"));
if (column7.Text.Equals("Suspended") && column9.Text.Equals("Publishing Failed"))
Assert.Fail("Failed because column8 is 'Suspended' and column10 is 'Publishing Failed'");
else
Assert.Pass();
}
Please note that this code will stop testing when it has found the "Suspended" and "Publishing Failed". If you want to continue testing until the final row in table, you have to use multiple assertions. NUnit, is it possible to continue executing test after Assert fails?

Remove tag from HtmlTextWriter

I am receiving text to a function public void LoadMarkup(HtmlTextWriter writer){} and I need to remove last <tr> tag from it.
The text I am getting is:
<table class="form">
<tr>
<td class="createLabel">Name:<span class="field">*</span></td><td><input type="text" maxlength="200" id="productTitle" name="productTitle" class="required" style="width: 250px;" /></td>
</tr><tr>
<td class="createLabel">Account:<span class="field">*</span></td><td><select id="recognizedAccount" name="recognizedAccount" class="required">
<option value="8a1dd4c7-8ddd-4632-aa81-6be29dc86d13">
Donation
</option><option value="5617660d-4794-4cad-aab8-a04323faf68a">
Dues Account
</option>
</select></td>
</tr><tr style="display: none;">
<td class="createLabel">Deferral Method:</td><td><select id="deferralMethod" name="deferralMethod" class="required">
<option value="e816071c-282d-46af-8c5d-ebecb8472b94">
None
</option>
</select></td>
</tr><tr>
<td class="createLabel">Description:<span class="required-field">*</span></td><td><textarea id="productDescription" name="productDescription" class="required" rows="3" style="width: 250px;"></textarea></td>
</tr><tr>
<td class="createLabel">Active:</td><td><input type="checkbox" id="productActive" name="productActive" /></td>
</tr><tr>
<td class="createLabel">Expiration Date:<span class="required-field">*</span></td><td><span class="datePicker"><input type="text" autocomplete="off" id="productExpirationDate" name="productExpirationDate" class="required" style="width: 135px;" /></span></td>
</tr><tr class="typeRates">
<td class="createLabel">Rate:<span class="required-field">*</span></td><td><input id="5a2da3f6-c3e5-4cd0-a194-7a704c8f07e0_rate" name="5a2da3f6-c3e5-4cd0-a194-7a704c8f07e0_rate" class="required" type="text" /></td>
</tr>
I need to remove this text from Upper text:
<tr class="typeRates">
<td class="createLabel">Rate:<span class="required-field">*</span></td><td><input id="5a2da3f6-c3e5-4cd0-a194-7a704c8f07e0_rate" name="5a2da3f6-c3e5-4cd0-a194-7a704c8f07e0_rate" class="required" type="text" /></td>
</tr>
I can not modify text before I am receiving it in function as this is coming from framework. Any one can help with it please suggest.
Thanks.
Not tested, nor the most robust, but perhaps give it a try:
public string RemoveLastTableRow(HtmlTextWriter writer)
{
// Create an array which contains each table row split
string[] html = writer.ToString().Split(new string[] { "<tr>" }, StringSplitOptions.None);
// Return all the array indexes joined, except for the final index
return string.Join("", html.Take(html.Count() - 1));
}

Selenium C# Any alternative way of finding if element is present on browser other than IsElementPresent(By by)

I have a scenario where there are three distinct html parent elements
<tr class="s3288" data-id="s3288">
<tr class="s3288" data-id="s3288">
<tr class="s1" data-id="s1">
Two these elements have a child element
<a class="gylink mapvl" title="No Map Values" href="#">Map Values</a>
but third one does not have this child element.
in my test scenario, I have to click this child element if present.
Is there any function by which i can detect if this element is present?
I have used following function bug for some reason it does not work properly
function i used
protected bool IsElementPresent(By by)
{
try
{
wait.Until(drv => (drv.FindElement(by)));
return true;
}
catch
{
return false;
}
}
Here is how the html of browser looks like
<tr class="s1" data-id="s1">
<td class="ftc1">
<td class="ftc2">
<td class="ftc3 valuecol">
<td class="ftc4 headcol">
<ul class="multihead">
<li>
<input class="txtinput chghead headcolfield pholder changit requiref" type="text" placeholder="Enter Column Header Value" validate="{required: true, messages: {required: 'Required'}}" name="th_s1_0_true" value="">
<a class="gylink mapvl" title="No Map Values" href="#">Map Values</a>
</li>
</ul>
</td>
<td class="ftc5 deletecol"> </td>
</tr>
<tr class="s3288" data-id="s3288">
<td class="ftc1">
<td class="ftc2">
<td class="ftc3 valuecol">
<td class="ftc4 headcol">
<ul class="multihead">
<li>
<input class="txtinput chghead headcolfield pholder changit requiref" type="text" placeholder="Enter Column Header Value" validate="{required: true, messages: {required: 'Required'}}" name="th_s3288_0_true" value="">
<a class="gylink mapvl" title="No Map Values" href="#">Map Values</a>
</li>
</ul>
</td>
<td class="ftc5 deletecol"> </td>
</tr>
<tr class="t1" data-id="t1">
<td class="ftc1">
<td class="ftc2">
<td class="ftc3 valuecol">
<td class="ftc4 headcol">
<ul class="multihead">
<li>
<input class="txtinput chghead headcolfield pholder changit requiref" type="text" placeholder="Enter Column Header Value" validate="{required: true, messages: {required: 'Required'}}" name="th_t1_0_true" value="">
</li>
</ul>
</td>
<td class="ftc5 deletecol"> </td>
</tr>

Categories

Resources