How to select the second tr of my HTML?
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%">
</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#
add a comment |
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%">
</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#
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
add a comment |
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%">
</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#
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%">
</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#
selenium-webdriver c# selenium-webdriver-c#
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
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
add a comment |
I made the following change and it worked:
portalweb = secondRow.FindElement(By.CssSelector("td:nth-of-type(4)")).Text;
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
I made the following change and it worked:
portalweb = secondRow.FindElement(By.CssSelector("td:nth-of-type(4)")).Text;
add a comment |
I made the following change and it worked:
portalweb = secondRow.FindElement(By.CssSelector("td:nth-of-type(4)")).Text;
add a comment |
I made the following change and it worked:
portalweb = secondRow.FindElement(By.CssSelector("td:nth-of-type(4)")).Text;
I made the following change and it worked:
portalweb = secondRow.FindElement(By.CssSelector("td:nth-of-type(4)")).Text;
answered Jan 10 at 13:23


Ronison MatosRonison Matos
766
766
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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