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]
1
  • 2
    Share your current code, HTML sample, desired output Commented Nov 15, 2018 at 12:00

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

14 Comments

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.
@Yarza Checkout my updated answer and let me know the status
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"]
Checkout my updated answer and let me know the status
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]"}"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.