Skip to content Skip to sidebar Skip to footer

Iterating Through A Table In Selenium Very Slow

I have a selenium python script that reads a table on a page. The table has 3 columns, the first is a list of IDs and the 3rd is a check box. I iterate through the IDs until I find

Solution 1:

You can find the relevant checkbox in one go by using an xpath expression to search a question node by text and to get it's td following sibling and input inside it:

checkbox = driver.find_element_by_xpath('//tr/td[1][(@class="rowodd" or @class="roweven") and text() = "%s${nbsp}"]/following-sibling::td[2]/input[starts-with(@name, "selectionIndex")]' % k)
checkbox.click()

Note that it would throw NoSuchElementException in case a question and a related to it checkbox is not found. You probably need to catch the exception:

try:
    checkbox = driver.find_element_by_xpath('//tr/td[1][(@class="rowodd" or @class="roweven") and text() = "%s${nbsp}"]/following-sibling::td[2]/input[starts-with(@name, "selectionIndex")]' % k)
    checkbox.click()
except NoSuchElementException:
    # question not found - need to handle it, or just move on?
    pass

Post a Comment for "Iterating Through A Table In Selenium Very Slow"