How to select the second tr of my HTML?












3















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;









share|improve this question




















  • 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
















3















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;









share|improve this question




















  • 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














3












3








3








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;









share|improve this question
















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;






selenium-webdriver c# selenium-webdriver-c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 10 at 17:39









Community

1




1










asked Jan 10 at 12:59









Ronison MatosRonison Matos

766




766








  • 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














  • 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








1




1





What result does your code give you?

– Kate Paulk
Jan 10 at 13:05





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





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










2 Answers
2






active

oldest

votes


















5














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.






share|improve this answer



















  • 1





    Exactly so, thank you for the explanation. Once again I am grateful for your help.

    – Ronison Matos
    Jan 10 at 13:46











  • 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;





share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "244"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsqa.stackexchange.com%2fquestions%2f37264%2fhow-to-select-the-second-tr-of-my-html%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    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.






    share|improve this answer



















    • 1





      Exactly so, thank you for the explanation. Once again I am grateful for your help.

      – Ronison Matos
      Jan 10 at 13:46











    • 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


















    5














    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.






    share|improve this answer



















    • 1





      Exactly so, thank you for the explanation. Once again I am grateful for your help.

      – Ronison Matos
      Jan 10 at 13:46











    • 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
















    5












    5








    5







    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.






    share|improve this answer













    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 10 at 13:35









    Bence KaulicsBence Kaulics

    680217




    680217








    • 1





      Exactly so, thank you for the explanation. Once again I am grateful for your help.

      – Ronison Matos
      Jan 10 at 13:46











    • 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
















    • 1





      Exactly so, thank you for the explanation. Once again I am grateful for your help.

      – Ronison Matos
      Jan 10 at 13:46











    • 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










    1




    1





    Exactly so, thank you for the explanation. Once again I am grateful for your help.

    – Ronison Matos
    Jan 10 at 13:46





    Exactly so, thank you for the explanation. Once again I am grateful for your help.

    – Ronison Matos
    Jan 10 at 13:46













    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





    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







    @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;





    share|improve this answer




























      2














      I made the following change and it worked:



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





      share|improve this answer


























        2












        2








        2







        I made the following change and it worked:



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





        share|improve this answer













        I made the following change and it worked:



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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 10 at 13:23









        Ronison MatosRonison Matos

        766




        766






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Software Quality Assurance & Testing Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsqa.stackexchange.com%2fquestions%2f37264%2fhow-to-select-the-second-tr-of-my-html%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith