How to reference HTML elements without ID in C# Windows Forms

Asked

Viewed 355 times

1

Good night!

I have the following code:

<H3>Dados Cadastrais</H3>
  <table class='dados'>
  <tr>
    <th>Avalista:</th>
    <td>VINICIUS ALVES GONZALEZ</td>
    <th>Contrato:</th>
    <td>72001018  </td>
  </tr>
  <tr>

How can I reference this element using C# to get the client name inside the <td> and the contract number tbm ...

I can only handle items that have ID using document.getElementById

2 answers

2

Have you tried using the Anglesharp

Install-Package Anglesharp

var xml = @"
<H3>Dados Cadastrais</H3>
<table class='dados'>
<tr>
    <th>Avalista:</th>
    <td>VINICIUS ALVES GONZALEZ</td>
    <th>Contrato:</th>
    <td>72001018  </td>
</tr>";


var parser = new HtmlParser();
var document = parser.Parse(xml);

Console.WriteLine(document.QuerySelector("td:nth-child(2)").InnerHtml);
Console.WriteLine(document.QuerySelector("td:nth-child(4)").InnerHtml);

outworking:

VINICIUS ALVES GONZALEZ
72001018  

0

You mean picking up the DOM elements using JavaScript and then send this data to C#?

You can use in addition to document.getElementById(), the document.getElementsByTagName("td")[0] note that the index [0] is why in your case would return both td's.

There is also the document.getElementsByClassName("nome") note that for this to work, you would have to in your html change the field td where is the name for <td class="nome">VINICIUS ALVES GONZALEZ</td>.

You can still take elements by the values and any other attribute you want, only you’ll need a more complex function for that. This function will first take all the elements and check in all if there is the attribute you want and if the value of that attribute is the same as you want.

  • In this case, Cod HTML is not mine, it’s a web page where I need to get these values... So in case I can get the "td" with the name I must locate within the "[0]" that varies, the value?

  • Yes, it will be a great challenge for you to identify that this is the name and not another given, since the tags are without identifiers. Imagine that this page uses more than one table, so yours document.getElementsByTagName("td") would return all the td of the full page. Dai in this case, the solution would be to get one element inside another already identified. I noticed that the table containing this data has a data class. Soon your code would look something like this. document.getElementsByClass("dados")[0].getElementsByTagName("td")[0];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.