2

I want to know how to get the HTML tag information and write to a C# project variable. Remember that I can not change the HTML because I do not have access.

In the example below, it is necessary to save the following fields in the variables: Client, Company, Portal (as marked in the image)

enter image description here

This is the HTML code:

<div style="font-size: 10pt; font-weight: bold">Portais
</div>
<hr>
<form name="Form1" method="post">
  <input type="hidden" name="operacao" value="1">
  <input type="hidden" name="d" value="">
  <input type="hidden" name="equipe" value="">
  <input type="text" name="conteudo" maxlength="200" size="20" onkeyup="escolhe_opcao_pesquisa(this);">
  <select name="opcao_pesquisa">
    <option value="A">Ambiente</option>
    <option value="C" selected="">CNPJ</option>         
    <option value="E">Nome do Portal</option>
    <option value="P">Número do Portal</option>
    <option value="R">Rede</option>
    <option value="L">Release</option>
    <option value="S">Sufixo Login</option>         
  </select>
  <input type="submit" value="Pesquisar &gt;">
</form>
<script>if(''!='') { document.Form2.endereco_ip.value='';}</script> 
<div style="font-size: 10pt; font-weight: bold">Listagem
</div>
<hr style="height: 1px">
<table width="100%" cellpadding="2" cellspacing="1" border="1" style="border-collapse: collapse">
  <tbody>
    <tr style="background-color: #E8E8E8">
      <td width="1%">&nbsp;
      </td>
      <td width="5%">
        <b>Rede
        </b>
      </td>
      <td width="5%">
        <b>Tipo
        </b>
      </td>
      <td width="5%">
        <b>Portal
        </b>
      </td>
      <td width="20%">
        <b>Nome do Portal
        </b>
      </td>
      <td width="10%">
        <b>Franqueador
        </b>
      </td>
      <td width="10%">
        <b>Ambiente
        </b>
      </td>
      <td width="10%">
        <b>Endereço BD
        </b>
      </td>
      <td width="15%">
        <b>Endereço Páginas
        </b>
      </td>
      <td width="5%">
        <b>Release
        </b>
      </td>
      <td width="5%">
        <strong>Administrador</strong>
      </td>
    </tr>
    <tr>
      <td bgcolor="green" title="xxx">
      </td>
      <td>Nome_emp
      </td>
      <td>Cliente
      </td>                    
      <td>
        <a target="_blank" title="Ao clicar no link abrirá a tela de consulta do módulo comercial." href="#">1149</a> 
        <img src="./Portais - MicrovixERP(empresa)_files/s_01blue.gif" title="&quot;Chave de Acesso&quot; para confirmar identidade de analista : 2179">
      </td>                                                     
      <td>Nome_emp 
        <b> - Empresa: 19; 
        </b>
      </td>
      <td>9841 - Nome - empresa
      </td>
      <td>Microvix A (95%)
      </td>
      <td>LINK.COM.BR
      </td>
      <td>
        <a href="http://#" target="_blank"><font color="blue">http://#
          </font></a>
      </td>
      <td>2.0
      </td>
      <td class="style1">

Here is my current code:

// Declaration of the global variable portalweb
public string portalweb;

// Button with function
public void btnCcnpj_Click_1(object sender, EventArgs e)
{
    IWebDriver driver = new ChromeDriver();
    driver.Navigate().GoToUrl("http://...");
    IWebElement secondRow = driver.FindElement(By.CssSelector("tr:nth-of-type(2)"));
    IWebElement client = secondRow.FindElement(By.CssSelector("td:nth-of-type(3)"));
    IWebElement portal = secondRow.FindElement(By.CssSelector("td:nth-of-type(4) a"));
    IWebElement nomeDoPortal = secondRow.FindElement(By.CssSelector("td:nth-of-type(5)"));
    portalweb = portal;
}

// TextBox where the HTML data should appear (in this example the number: 1149)
private void txbPortal_TextChanged(object sender, EventArgs e)
{
    txbPortal.Text = portalweb;
}

But I am getting the following error:

Error CS0029 Can not implicitly convert type OpenQA.Selenium.IWebElement em string

  • if i am getting it right then your solution may be this element.GetAttribute("innerHTML"); – Rao Jan 8 at 6:39
  • The img tag is not closed - could you double check if the HTML is valid? – João Farias Jan 8 at 6:41
  • About your error, you are trying to assign an IWebElement to a string. You should assign the text of the web element instead, like portalweb = portal.Text – Bence Kaulics Jan 8 at 20:57
1

First of all, we need to find the second row:

IWebElement secondRow = driver.FindElement(By.CssSelector("tr:nth-of-type(2)"));

Then, you can find each element by position:

IWebElement client = secondRow.FindElement(By.CssSelector("td:nth-of-type(3)"));
IWebElement portal = secondRow.FindElement(By.CssSelector("td:nth-of-type(4) a"));
IWebElement nomeDoPortal = secondRow.FindElement(By.CssSelector("td:nth-of-type(5)"));

By extracting these into functions, you can re-use them to iterate over a list of companies.

1

You can write a generic xPath expression to get the value of the marked columns in an array even for multiple rows like:

//tr[boolean(position() != 1)]/td[position() = 3 or position() = 4 or position() = 5]

This expression will skip the first row, which is the header, and then return the value of column 3, 4 and 5 for the rest of the table.

// Find multiple elements
IReadOnlyCollection<IWebElement> columns = this.driver.FindElements(By.XPath("//tr[boolean(position() != 1)]/td[position() = 3 or position() = 4 or position() = 5]"));

Drawback is that data for all rows will be stored in one array, and your have to get them by groups of three. On the other hand this way you do not need to know how much rows are in the table.

If the row numbers are known you can replace the boolean function in the tr to get only the three fields for a given row.

// Find multiple elements
IReadOnlyCollection<IWebElement> columns = this.driver.FindElements(By.XPath("//tr[position() = 2]/td[position() = 3 or position() = 4 or position() = 5]"));

// to get the actual text values for the three cells in row 2
string client = columns.ElementAt(0).Text
string portal = columns.ElementAt(1).Text
string nomeDoPortal = columns.ElementAt(2).Text
1

It looks like you've got the elements OK, but you can't convert elements to strings.

You need to use the .Text property to get the text from the element, and then use C#'s string functions to get what you need out of it.

string clientName = client.Text;
int portalNumber = Int32.Parse(portal.Text);
int nomeDePortalNumber = Int32.Parse(System.Text.RegularExpressions.Regex.Replace(nomeDePortal.Text, "^[^0-9]*", String.Empty).Replace(";", String.Empty));

That should let you manipulate those as you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.