4

The code below works but it only selects the first tr:

My HTML:

<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="Descricao." href="#">1149</a>                                                     
      <td>Nome_emp 
        <b> - Empresa: 19; 
        </b>
      </td>
      <td>9841 - Nome - empresa
      </td>
      <td>Micro (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">
      </td>
    </tr>
  </tbody>
</table>

My Selenium code:

driver.Navigate().GoToUrl("http://...");
IWebElement secondRow = driver.FindElement(By.CssSelector("table tr:nth-of-type(2)"));
portalweb = driver.FindElement(By.CssSelector("td:nth-of-type(4)")).Text;
txbPortal.Text = portalweb;
  • 1
    What result does your code give you? – Kate Paulk Jan 10 at 13:05
  • When I run it returns the title "Portal" being that it should return the value "1149" which is in the second tr. – Ronison Matos Jan 10 at 13:10
6

To add explanation to what you have already figured out. The problem was that with this line you correctly acquired the second row:

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

but with the next piece of code, you did not get the 4th cell of the second row, but the first 4th cell of the DOM and that starts from the first row, the header. That is why it has returned "portal".

portalweb = driver.FindElement(By.CssSelector("td:nth-of-type(4)")).Text;

so instead of the previous line you should do what you have already figured out:

portalweb = secondRow.FindElement(By.CssSelector("td:nth-of-type(4)")).Text;

run the second FindElement on the second row, and not on the whole DOM again.

  • Why css is used in this case? any specific reason? I think we can achieve it by Xpath or filndelementsby tagname and 2nd element from list. Please clarify – SQA_LEARN Jan 13 at 2:06
  • @SQA_LEARN The OP used css I just used his original code in the answer. Anyway the OP used css because it was suggested to him in his other question: sqa.stackexchange.com/questions/37202/… I specially suggested the same as you, FindElements and xpath. – Bence Kaulics Jan 13 at 10:03
2

I made the following change and it worked:

portalweb = secondRow.FindElement(By.CssSelector("td:nth-of-type(4)")).Text;

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.