Python Selenium - How can I click on the third last element from a group of elements in a table?












-1















Good day everyone,
Can someone explain to me how I cant get the number of elements in a table? I am running through multiple tables that all have different lengths and I always need the 3rd last item. I have the xpath of the table that contains those .



Using Selenium's Python API - How do I get the number of rows in a table?



I found this answer and some other answers that seem to answer my question, but to be honest: At this point I just dont understand how to implement this into my code.



I am using Python 3.6 and Selenium. Would be great if someone could help me out, since this little script could save me 1-2 hours of tedious work every day.



EDIT:



First I login to my Page, where I can search for CustomerIDs there I use the following Code:



Customer_IDs = ['100001','100002','100003']
for ID in Customer_IDs:

customer = browser.find_element_by_id('ContentPlaceHolder1_txtcustomercode')
customer.send_keys(ID)
customer.send_keys(Keys.TAB)
browser.find_element_by_id("ContentPlaceHolder1_btnsearch").click()

#here I want go through the table to find the amount of rows

browser.find_element_by_id("ContentPlaceHolder1_gridview1_refid_1").click()
browser.find_element_by_id('ContentPlaceHolder1_txtcustomercode').clear()


This "ContentPlaceHolder1_gridview1_refid_1" is basically what I need to change. If there are 10 Rows I would need "ContentPlaceHolder1_gridview1_refid_7" and so on.



Update A (from comments)



I have the xpath of the table where the file is stored. The code that I have written works fine when I want the first element of that table ("ContentPlaceHolder1_gridview1_refid_1"). But I need the third last element.



Update B (from comments)



The xpath of the table is:



//*[@id="ContentPlaceHolder1_gridview1"]


Update C (from comments)



The xpaths for the rows are:




  • //*[@id="ContentPlaceHolder1_gridview1"]/tbody/tr[1]


  • //*[@id="ContentPlaceHolder1_gridview1"]/tbody/tr[2]










share|improve this question




















  • 2





    Share your current code, HTML sample, desired output

    – Andersson
    Nov 15 '18 at 12:00
















-1















Good day everyone,
Can someone explain to me how I cant get the number of elements in a table? I am running through multiple tables that all have different lengths and I always need the 3rd last item. I have the xpath of the table that contains those .



Using Selenium's Python API - How do I get the number of rows in a table?



I found this answer and some other answers that seem to answer my question, but to be honest: At this point I just dont understand how to implement this into my code.



I am using Python 3.6 and Selenium. Would be great if someone could help me out, since this little script could save me 1-2 hours of tedious work every day.



EDIT:



First I login to my Page, where I can search for CustomerIDs there I use the following Code:



Customer_IDs = ['100001','100002','100003']
for ID in Customer_IDs:

customer = browser.find_element_by_id('ContentPlaceHolder1_txtcustomercode')
customer.send_keys(ID)
customer.send_keys(Keys.TAB)
browser.find_element_by_id("ContentPlaceHolder1_btnsearch").click()

#here I want go through the table to find the amount of rows

browser.find_element_by_id("ContentPlaceHolder1_gridview1_refid_1").click()
browser.find_element_by_id('ContentPlaceHolder1_txtcustomercode').clear()


This "ContentPlaceHolder1_gridview1_refid_1" is basically what I need to change. If there are 10 Rows I would need "ContentPlaceHolder1_gridview1_refid_7" and so on.



Update A (from comments)



I have the xpath of the table where the file is stored. The code that I have written works fine when I want the first element of that table ("ContentPlaceHolder1_gridview1_refid_1"). But I need the third last element.



Update B (from comments)



The xpath of the table is:



//*[@id="ContentPlaceHolder1_gridview1"]


Update C (from comments)



The xpaths for the rows are:




  • //*[@id="ContentPlaceHolder1_gridview1"]/tbody/tr[1]


  • //*[@id="ContentPlaceHolder1_gridview1"]/tbody/tr[2]










share|improve this question




















  • 2





    Share your current code, HTML sample, desired output

    – Andersson
    Nov 15 '18 at 12:00














-1












-1








-1








Good day everyone,
Can someone explain to me how I cant get the number of elements in a table? I am running through multiple tables that all have different lengths and I always need the 3rd last item. I have the xpath of the table that contains those .



Using Selenium's Python API - How do I get the number of rows in a table?



I found this answer and some other answers that seem to answer my question, but to be honest: At this point I just dont understand how to implement this into my code.



I am using Python 3.6 and Selenium. Would be great if someone could help me out, since this little script could save me 1-2 hours of tedious work every day.



EDIT:



First I login to my Page, where I can search for CustomerIDs there I use the following Code:



Customer_IDs = ['100001','100002','100003']
for ID in Customer_IDs:

customer = browser.find_element_by_id('ContentPlaceHolder1_txtcustomercode')
customer.send_keys(ID)
customer.send_keys(Keys.TAB)
browser.find_element_by_id("ContentPlaceHolder1_btnsearch").click()

#here I want go through the table to find the amount of rows

browser.find_element_by_id("ContentPlaceHolder1_gridview1_refid_1").click()
browser.find_element_by_id('ContentPlaceHolder1_txtcustomercode').clear()


This "ContentPlaceHolder1_gridview1_refid_1" is basically what I need to change. If there are 10 Rows I would need "ContentPlaceHolder1_gridview1_refid_7" and so on.



Update A (from comments)



I have the xpath of the table where the file is stored. The code that I have written works fine when I want the first element of that table ("ContentPlaceHolder1_gridview1_refid_1"). But I need the third last element.



Update B (from comments)



The xpath of the table is:



//*[@id="ContentPlaceHolder1_gridview1"]


Update C (from comments)



The xpaths for the rows are:




  • //*[@id="ContentPlaceHolder1_gridview1"]/tbody/tr[1]


  • //*[@id="ContentPlaceHolder1_gridview1"]/tbody/tr[2]










share|improve this question
















Good day everyone,
Can someone explain to me how I cant get the number of elements in a table? I am running through multiple tables that all have different lengths and I always need the 3rd last item. I have the xpath of the table that contains those .



Using Selenium's Python API - How do I get the number of rows in a table?



I found this answer and some other answers that seem to answer my question, but to be honest: At this point I just dont understand how to implement this into my code.



I am using Python 3.6 and Selenium. Would be great if someone could help me out, since this little script could save me 1-2 hours of tedious work every day.



EDIT:



First I login to my Page, where I can search for CustomerIDs there I use the following Code:



Customer_IDs = ['100001','100002','100003']
for ID in Customer_IDs:

customer = browser.find_element_by_id('ContentPlaceHolder1_txtcustomercode')
customer.send_keys(ID)
customer.send_keys(Keys.TAB)
browser.find_element_by_id("ContentPlaceHolder1_btnsearch").click()

#here I want go through the table to find the amount of rows

browser.find_element_by_id("ContentPlaceHolder1_gridview1_refid_1").click()
browser.find_element_by_id('ContentPlaceHolder1_txtcustomercode').clear()


This "ContentPlaceHolder1_gridview1_refid_1" is basically what I need to change. If there are 10 Rows I would need "ContentPlaceHolder1_gridview1_refid_7" and so on.



Update A (from comments)



I have the xpath of the table where the file is stored. The code that I have written works fine when I want the first element of that table ("ContentPlaceHolder1_gridview1_refid_1"). But I need the third last element.



Update B (from comments)



The xpath of the table is:



//*[@id="ContentPlaceHolder1_gridview1"]


Update C (from comments)



The xpaths for the rows are:




  • //*[@id="ContentPlaceHolder1_gridview1"]/tbody/tr[1]


  • //*[@id="ContentPlaceHolder1_gridview1"]/tbody/tr[2]







python selenium selenium-webdriver xpath webdriver






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 14:09









DebanjanB

43.9k114386




43.9k114386










asked Nov 15 '18 at 11:43









Micha BruggerMicha Brugger

236




236








  • 2





    Share your current code, HTML sample, desired output

    – Andersson
    Nov 15 '18 at 12:00














  • 2





    Share your current code, HTML sample, desired output

    – Andersson
    Nov 15 '18 at 12:00








2




2





Share your current code, HTML sample, desired output

– Andersson
Nov 15 '18 at 12:00





Share your current code, HTML sample, desired output

– Andersson
Nov 15 '18 at 12:00












1 Answer
1






active

oldest

votes


















2














To locate the third last row element with id starting as ContentPlaceHolder1_gridview1_refid_ you can use the following xpath:



browser.find_element_by_xpath("//*[@id='ContentPlaceHolder1_gridview1']/tbody/tr[last()-3]").click()





share|improve this answer


























  • I'm not sure I understand your answer correctly. I have the xpath of the table where the file is stored. The code that I have written works fine when I want the first element of that table ("ContentPlaceHolder1_gridview1_refid_1"). But I need the third last element.

    – Micha Brugger
    Nov 15 '18 at 12:47











  • @Yarza Checkout my updated answer and let me know the status

    – DebanjanB
    Nov 15 '18 at 12:52













  • im getting an error message, but maybe I'm just too stupid to implement it properly. the xpath of the table is: //*[@id="ContentPlaceHolder1_gridview1"]

    – Micha Brugger
    Nov 15 '18 at 13:39













  • Checkout my updated answer and let me know the status

    – DebanjanB
    Nov 15 '18 at 13:47











  • implemented exactly your comment into my existing code - got the following error message: "selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[starts-with(@id,'ContentPlaceHolder1_gridview')][last()-3]"}"

    – Micha Brugger
    Nov 15 '18 at 13:50











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53318738%2fpython-selenium-how-can-i-click-on-the-third-last-element-from-a-group-of-elem%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














To locate the third last row element with id starting as ContentPlaceHolder1_gridview1_refid_ you can use the following xpath:



browser.find_element_by_xpath("//*[@id='ContentPlaceHolder1_gridview1']/tbody/tr[last()-3]").click()





share|improve this answer


























  • I'm not sure I understand your answer correctly. I have the xpath of the table where the file is stored. The code that I have written works fine when I want the first element of that table ("ContentPlaceHolder1_gridview1_refid_1"). But I need the third last element.

    – Micha Brugger
    Nov 15 '18 at 12:47











  • @Yarza Checkout my updated answer and let me know the status

    – DebanjanB
    Nov 15 '18 at 12:52













  • im getting an error message, but maybe I'm just too stupid to implement it properly. the xpath of the table is: //*[@id="ContentPlaceHolder1_gridview1"]

    – Micha Brugger
    Nov 15 '18 at 13:39













  • Checkout my updated answer and let me know the status

    – DebanjanB
    Nov 15 '18 at 13:47











  • implemented exactly your comment into my existing code - got the following error message: "selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[starts-with(@id,'ContentPlaceHolder1_gridview')][last()-3]"}"

    – Micha Brugger
    Nov 15 '18 at 13:50
















2














To locate the third last row element with id starting as ContentPlaceHolder1_gridview1_refid_ you can use the following xpath:



browser.find_element_by_xpath("//*[@id='ContentPlaceHolder1_gridview1']/tbody/tr[last()-3]").click()





share|improve this answer


























  • I'm not sure I understand your answer correctly. I have the xpath of the table where the file is stored. The code that I have written works fine when I want the first element of that table ("ContentPlaceHolder1_gridview1_refid_1"). But I need the third last element.

    – Micha Brugger
    Nov 15 '18 at 12:47











  • @Yarza Checkout my updated answer and let me know the status

    – DebanjanB
    Nov 15 '18 at 12:52













  • im getting an error message, but maybe I'm just too stupid to implement it properly. the xpath of the table is: //*[@id="ContentPlaceHolder1_gridview1"]

    – Micha Brugger
    Nov 15 '18 at 13:39













  • Checkout my updated answer and let me know the status

    – DebanjanB
    Nov 15 '18 at 13:47











  • implemented exactly your comment into my existing code - got the following error message: "selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[starts-with(@id,'ContentPlaceHolder1_gridview')][last()-3]"}"

    – Micha Brugger
    Nov 15 '18 at 13:50














2












2








2







To locate the third last row element with id starting as ContentPlaceHolder1_gridview1_refid_ you can use the following xpath:



browser.find_element_by_xpath("//*[@id='ContentPlaceHolder1_gridview1']/tbody/tr[last()-3]").click()





share|improve this answer















To locate the third last row element with id starting as ContentPlaceHolder1_gridview1_refid_ you can use the following xpath:



browser.find_element_by_xpath("//*[@id='ContentPlaceHolder1_gridview1']/tbody/tr[last()-3]").click()






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 14:11

























answered Nov 15 '18 at 12:41









DebanjanBDebanjanB

43.9k114386




43.9k114386













  • I'm not sure I understand your answer correctly. I have the xpath of the table where the file is stored. The code that I have written works fine when I want the first element of that table ("ContentPlaceHolder1_gridview1_refid_1"). But I need the third last element.

    – Micha Brugger
    Nov 15 '18 at 12:47











  • @Yarza Checkout my updated answer and let me know the status

    – DebanjanB
    Nov 15 '18 at 12:52













  • im getting an error message, but maybe I'm just too stupid to implement it properly. the xpath of the table is: //*[@id="ContentPlaceHolder1_gridview1"]

    – Micha Brugger
    Nov 15 '18 at 13:39













  • Checkout my updated answer and let me know the status

    – DebanjanB
    Nov 15 '18 at 13:47











  • implemented exactly your comment into my existing code - got the following error message: "selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[starts-with(@id,'ContentPlaceHolder1_gridview')][last()-3]"}"

    – Micha Brugger
    Nov 15 '18 at 13:50



















  • I'm not sure I understand your answer correctly. I have the xpath of the table where the file is stored. The code that I have written works fine when I want the first element of that table ("ContentPlaceHolder1_gridview1_refid_1"). But I need the third last element.

    – Micha Brugger
    Nov 15 '18 at 12:47











  • @Yarza Checkout my updated answer and let me know the status

    – DebanjanB
    Nov 15 '18 at 12:52













  • im getting an error message, but maybe I'm just too stupid to implement it properly. the xpath of the table is: //*[@id="ContentPlaceHolder1_gridview1"]

    – Micha Brugger
    Nov 15 '18 at 13:39













  • Checkout my updated answer and let me know the status

    – DebanjanB
    Nov 15 '18 at 13:47











  • implemented exactly your comment into my existing code - got the following error message: "selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[starts-with(@id,'ContentPlaceHolder1_gridview')][last()-3]"}"

    – Micha Brugger
    Nov 15 '18 at 13:50

















I'm not sure I understand your answer correctly. I have the xpath of the table where the file is stored. The code that I have written works fine when I want the first element of that table ("ContentPlaceHolder1_gridview1_refid_1"). But I need the third last element.

– Micha Brugger
Nov 15 '18 at 12:47





I'm not sure I understand your answer correctly. I have the xpath of the table where the file is stored. The code that I have written works fine when I want the first element of that table ("ContentPlaceHolder1_gridview1_refid_1"). But I need the third last element.

– Micha Brugger
Nov 15 '18 at 12:47













@Yarza Checkout my updated answer and let me know the status

– DebanjanB
Nov 15 '18 at 12:52







@Yarza Checkout my updated answer and let me know the status

– DebanjanB
Nov 15 '18 at 12:52















im getting an error message, but maybe I'm just too stupid to implement it properly. the xpath of the table is: //*[@id="ContentPlaceHolder1_gridview1"]

– Micha Brugger
Nov 15 '18 at 13:39







im getting an error message, but maybe I'm just too stupid to implement it properly. the xpath of the table is: //*[@id="ContentPlaceHolder1_gridview1"]

– Micha Brugger
Nov 15 '18 at 13:39















Checkout my updated answer and let me know the status

– DebanjanB
Nov 15 '18 at 13:47





Checkout my updated answer and let me know the status

– DebanjanB
Nov 15 '18 at 13:47













implemented exactly your comment into my existing code - got the following error message: "selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[starts-with(@id,'ContentPlaceHolder1_gridview')][last()-3]"}"

– Micha Brugger
Nov 15 '18 at 13:50





implemented exactly your comment into my existing code - got the following error message: "selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[starts-with(@id,'ContentPlaceHolder1_gridview')][last()-3]"}"

– Micha Brugger
Nov 15 '18 at 13:50




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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%2fstackoverflow.com%2fquestions%2f53318738%2fpython-selenium-how-can-i-click-on-the-third-last-element-from-a-group-of-elem%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

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma