Home > Back-end >  How to efficiently iterate table number in Selenium Webdriver using Python?
How to efficiently iterate table number in Selenium Webdriver using Python?

Time:10-26

Please suggest me how to changes efficiently tr[1], [2], [3] or N numbers, following the table provided on the website automatically

code :

browser.find_element_by_xpath('//*[@id="event"]/tbody/tr[1]/td[9]').click()
browser.find_element_by_xpath('//*[@id="event"]/tbody/tr[2]/td[9]').click()
browser.find_element_by_xpath('//*[@id="event"]/tbody/tr[3]/td[9]').click()

CodePudding user response:

You can consider using find_elements_by_xpath('.//tr') instead of find_element_by_xpath.

Alternatively, you can use a simple for loop with a try-except. It's not the most elegant, but it will get you all N rows before going out due to the exception.

CodePudding user response:

Try like below:

options = browser.find_elements_by_xpath('//*[@id="event"]/tbody/tr') # this xpath should highlight all the tr tags in the DOM

for opt in options:
    opt.find_element_by_xpath('./td[9]') # this should get the 9th td tag of that particular tr. Use a "." in the xpath to find element within an element.
  • Related