问题
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:
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()
来源:https://stackoverflow.com/questions/53318738/python-selenium-how-can-i-click-on-the-third-last-element-from-a-group-of-elem