0

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]
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Micha Brugger
  • 359
  • 3
  • 22

1 Answers1

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()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 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 – undetected Selenium 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 – undetected Selenium 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
  • When you land on the page and face the error, can you verify in the Development tools if the elements `ContentPlaceHolder1_gridview1`, `ContentPlaceHolder1_gridview2` ... `ContentPlaceHolder1_gridview4` exists at-least? – undetected Selenium Nov 15 '18 at 13:54
  • And thanks for updating my question, I'm really kind of new to programming and stackoverflow. Will try to do better next time! – Micha Brugger Nov 15 '18 at 13:55
  • It's fine with me. Need your help to nail your issue at the earliest :) – undetected Selenium Nov 15 '18 at 13:56
  • Thanks a lot :) To be honest, I dont fully understand your answer. I dont think there are ContentPlaceHolder1_gridview2 and so on. The xpaths for the rows are: `//*[@id="ContentPlaceHolder1_gridview1"]/tbody/tr[1]`, `//*[@id="ContentPlaceHolder1_gridview1"]/tbody/tr[2]`, ... – Micha Brugger Nov 15 '18 at 14:06
  • Checkout my updated answer and let me know the status – undetected Selenium Nov 15 '18 at 14:11
  • I made a mistake, I'm sorry. The rows are called `[@id="ContentPlaceHolder1_gridview1"]/tbody/tr[1]` But I just realized that the actual elements I want to access are: `ContentPlaceHolder1_gridview1_refid_**XY**`. – Micha Brugger Nov 15 '18 at 14:40
  • Seems we are not reaching any where without the _HTML_. Update the question with the relevant _HTML_ for further analysis. – undetected Selenium Nov 15 '18 at 14:44
  • What exactly can I provide to help you? I'm currently inspecting the page. I can provide outerHTML, selector and xpath. Would that help you? – Micha Brugger Nov 15 '18 at 14:49
  • Is there no way to just count the rows in that table? So if it is for example 124 rows then I can click the element by just going for `"ContentPlaceHolder1_gridview1_refid_"&"124"` Or something similar. – Micha Brugger Nov 15 '18 at 15:12