Hello im trying to get this "a" atrribute from this HTML source code using HtmlAgilityPack in C#.
<table width='200'>
<tr>
<td width='50'>
<a href='index.php?action=shop&type=koszulka'>
<img src='images/lay_game/miasto/sklep.png' width='40' class="dymek" style='cursor:pointer;' title="Tutaj możesz kupić wyposażenie dla swojego zawodnika" /></a>
</td>
<td>
<a href='index.php?action=shop&type=koszulka' >Sklepy</a>
</td>
</tr>
<tr>
<td width='50'>
<a href='index.php?action=37317|lbr5tlbphafc3cf30b08vl8601|trening|MCMxIzI=|a32a443dd66c39e8cce9a4903171d81b|162f3a6d72c860855a5dc3de18c8855c'>
<img src='images/lay_game/miasto/trening.png' width='40' class="dymek" style='cursor:pointer;' title="Chcesz podnieść swoje umiejętności? Dobrze trafiłeś"/></a>
</td>
<td>
<a href='index.php?action=37317|lbr5tlbphafc3cf30b08vl8601|trening|MCMxIzI=|a32a443dd66c39e8cce9a4903171d81b|162f3a6d72c860855a5dc3de18c8855c'>Trening</a>
</td>
</tr>
<tr>
<td width='50'>
<a href='index.php?action=hospital'>
<img src='images/lay_game/miasto/szpital.png' width='40' class="dymek" style='cursor:pointer;' title="Możesz tu zredukować zmęczenie, wyleczyć kontuzję lub podnieść formę"/></a>
</td>
<td>
<a href='index.php?action=hospital'>Szpital</a>
</td>
</tr>
<tr>
<td width='50'>
<a href='index.php?action=gielda'>
<img src='images/lay_game/miasto/centrum.png' width='40' class="dymek" style='cursor:pointer;' title="Chcesz zarobić i nie boisz się ryzyka? Zatem witamy na giełdzie FT" /></a>
</td>
<td>
<a href='index.php?action=gielda'>Giełda</a>
</td>
</tr>
<tr>
<td width='50'>
<a href='index.php?action=pojedynek'>
<img src='images/lay_game/miasto/pojedynek.png' width='40' class="dymek" style='cursor:pointer;' title="Pojedynek Uliczny." /></a>
</td>
<td>
<a href='index.php?action=pojedynek'>Pojedynek</a>
</td>
</tr>
</table>
My target is a attribute with href="index.php?action=37317|lbr5tlbphafc3cf30b08vl8601|trening|MCMxIzI=|a32a443dd66c39e8cce9a4903171d81b|162f3a6d72c860855a5dc3de18c8855c"
I really dunno how to get this. My trying code is below:
HtmlAgilityPack.HtmlDocument HTMLParser = new HtmlAgilityPack.HtmlDocument();
HTMLParser.LoadHtml(result);
string href;
foreach (HtmlNode node in HTMLParser.DocumentNode.SelectNodes("//table//tr//td//a"))
{
href = node.ChildNodes[0].InnerHtml;
}
But it not working :(
The following should work fine, assuming all you care about is that particular <a> tag:
HtmlNode anchor = HTMLParser.DocumentNode.SelectSingleNode(#"//table/tr[2]/td/a");
There are two <a> elements with href attribute you wanted. More importantly, it isn't clear how you want to identify that particular <a>. Assuming that you want to indentify by inner text "Trening", try this way :
HtmlNode a = HTMLParser.DocumentNode.SelectSingleNode(#"//table/tr/td/a[.='Trening']");
String href = a.GetAttributeValue("href", "");
Related
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();
I want to retrieve only the structure of HTML document using C# as there is a requirement to create a template from the document and store in a database,which can be used in the future to compare if such a document was earlier received and process further For eg if have the below simple HTML:
<HTML>
<BODY>
<DIV name="Span1">Simple HTML Form</DIV>
<FORM>
<SPAN name="TextLabel">EID: 12345</SPAN>
<SPAN name="TextLabel1">Date:'2019-07-10'</SPAN>
</FORM>
<table>
<tr>
<td>Name </td>
<td> Occupation</td>
</tr>
<tr>
<td> XYZ </td>
<td> SSE </td>
</tr>
</table>
</BODY>
</HTML>
I want the following output:
<HTML>
<BODY>
<DIV></DIV>
<FORM>
<SPAN></SPAN>
<SPAN></SPAN>
</FORM>
<table>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</BODY>
</HTML>
Using HtmlAgilityPack might be an option. You can start from this example and develop...
HtmlDocument doc = new HtmlDocument();
string html = #"<HTML>
<BODY>
<DIV name=""Span1"">Simple HTML Form</DIV>
<FORM>
<SPAN name=""TextLabel"">EID: 12345</SPAN>
<SPAN name=""TextLabel1"">Date:'2019-07-10'</SPAN>
</FORM>
<table>
<tr>
<td>Name </td>
<td> Occupation</td>
</tr>
<tr>
<td> XYZ </td>
<td> SSE </td>
</tr>
</table>
</BODY>
</HTML>";
doc.LoadHtml(html);
var nodes = doc.DocumentNode.Descendants();
you can use Regex :
string html = #"<HTML>
<BODY>
<DIV name=""Span1"">Simple HTML Form</DIV>
<FORM>
<SPAN name=""TextLabel"">EID: 12345</SPAN>
<SPAN name=""TextLabel1"">Date:'2019-07-10'</SPAN>
</FORM>
<table>
<tr>
<td>Name </td>
<td> Occupation</td>
</tr>
<tr>
<td> XYZ </td>
<td> SSE </td>
</tr>
</table>
</BODY>
</HTML>";
Regex regex = new Regex(#"<.+?>");
MatchCollection match = regex.Matches(html);
foreach(var item in match)
Console.WriteLine(item);
In My web page In a portion i want to display a text/message and that text/message has to change after 15 seconds and it has to replaced with another text/message in the same portion. I Created this web application using ASP.NET.
In above Image I want to Display the Text/Message. How can i do ?
ASPX :
<table>
<tr>
<td style="width: 150px">
<a href="http://www.wissen.com">
<img alt="" class="style4" src="Wissen_logo.png" />
</a>
</td>
<td style="width: 1000px; background-color:Aqua">
<marquee behavior="scroll" scrollamount="3" direction="left" width="1000">ghdkj * hchjsdgfhgflghl * yuftwefrweirgeweko</marquee>
</td>
</tr>
</table>
JS
<script type="text/javascript">
var i=1;
var stat1="foo";
var stat2="Bar";
var stat3="foofoo";
function showText(){
var msgNo="stat"+i;
msgNo=eval(msgNo);
var tgtLabel=document.getElementById("spnRandom");
tgtLabel.innerHTML=msgNo;
i=i+1;
if(i==4){
i=1;
}
}
window.onload=function(){
window.setInterval(showText,1000);
};
</script>
HTML
<table>
<tr>
<td style="width: 150px">
<a href="http://www.wissen.com">
<img alt="" class="style4" src="Wissen_logo.png" />
</a>
</td>
<td style="width: 1000px; background-color:Aqua">
<div>
<span id="spnRandom"></span>
</div>
</td>
</tr>
</table>
Here is a working Fiddle
If you want it for 15 second, just change the value from 1000 to 15000
Create UpdatePanel.
Create a Label inside UpdatePanel.
I have a XML that return, at some point, this:
<TESTO>
<img src="../path/image.jpg" alt="" />
</TESTO>
well, if I do:
string TESTO = m_oNode.SelectSingleNode("TESTO").InnerText;
TESTO will be "empty". Why? How can I read the whole text? With other tag without HTML tag all works perfectly...
I use XmlDocument
EDIT - code that create an Exception with InnerXml():
<TESTO>
<table style="width: 100%;" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td> </td>
<td width="700"><img src="/testata.jpg" alt="mycaf.it" width="700" height="333" border="0" /></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td style="text-align: center; background-color: #f5f5f5;" align="center" bgcolor="#f5f5f5"><br />
<p style="color: #ee2e24; font-style: italic; font-size: 25px; font-family: Arial;">portale<br /> </p>
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</tbody>
</table>
</TESTO>
InnerText gets only the Text (for mixed content or text content). Use InnerXml instead.
Example:
<A>
Some text in mixed content
<B>OnlyText</B>
</A
Gives the result:
InnerText = "Some text in mixed content\r\nOnlyText"
InnerXml = "Some text in mixed content\r\n<B>OnlyText</B>";
To read the content of an html element you have to use yourElement.innerXml instead of yourElement.InnerText
Per leggere il contenuto di un elemento html devi usare yourElement.innerXml al posto di yourElement.InnerText :)
I am using c# Web.Client to download an html string.
A small example of the html been returned is
<tbody class='resultBody ' id='Tbody2'>
<tr id='Tr2' class='firstRow'>
<td class='cbrow tier_Gold' rowspan='4'>
<input type='checkbox' name='listingId' value='452' id='Checkbox2' />
</td>
<td class='resNum' rowspan='4'>
<div class='node'>
B</div>
</td>
<td class='datarow busName' id='Td2'>
</td>
<td rowspan='2' class='resLinks'>
</td>
<td class="hoops" rowspan='2'>
</td>
</tr>
<tr>
<td class="datarow">
<dl class="addrBlock">
<dd class="bizAddr">
123 ABC St</dd>
</dl>
</td>
</tr>
</tbody>
<tbody class='resultBody ' id='Tbody3'>
<tr id='Tr3' class='firstRow'>
<td class='cbrow tier_Gold' rowspan='4'>
<input type='checkbox' name='listingId' value='99' id='Checkbox3' />
</td>
<td class='resNum' rowspan='4'>
<div class='node'>
B</div>
</td>
<td class='datarow busName' id='Td3'>
</td>
<td rowspan='2' class='resLinks'>
</td>
<td class="hoops" rowspan='2'>
</td>
</tr>
<tr>
<td class="datarow">
<dl class="addrBlock">
<dd class="bizAddr">
1111 Some St</dd>
</dl>
</td>
</tr>
</tbody>
I am interested in 2 elements of the html but I have no idea the best way to get to them. How would be the best way for me to get the value from and get the inner html from the element
Any suggestions would be great!!!
download the HTML Agility Pack (free)
create a new HtmlDocument
loadhtml
use DOM navigation or an xpath query (SelectSingleNode etc) to find the elements
access InerHtml of the elements you want
The API is similar to XmlDocument, but it works on html that isn't xhtml.