Micha Brugger
Micha Brugger

Reputation: 404

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

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:

Upvotes: 0

Views: 1532

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193188

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()

Upvotes: 2

Related Questions