Skip to content
Advertisement

How to loop if the page automatically refresh after loop 1 and don’t want to continue loop 2 in Selenium

Please suggest me how to loop for tr[1], [2], [3] or N numbers, following the table provided on the website automatically if the page automatically refresh after loop 1 and don’t want to continue loop 2, loop 3, loop N?

so like this for example: loop 1 (page auto refresh) > loop 2 (page auto refresh) > loop N (page auto refresh) | *This auto refresh page is directly from the website, not from the code

code:

try:
    while True:
        browser.get('URL')
        options = browser.find_elements_by_xpath('//*[@id="event"]/tbody/tr')
        for opt in options:
            opt.find_element_by_xpath('./td[9]').click()
            time.sleep(2.5)
            break
except (ElementNotVisibleException, WebDriverException, NoSuchElementException):
        pass

here is a loop while for iterating continuously when the for loop has reached the loop N on the website I suggest there is an error in the loop for, but I don’t know where it is so that I can continue the loop

Advertisement

Answer

Assuming that the page refreshes after the click operation on the td tags, for the next iteration the element options is no longer found(Since the page got refreshed).

Try to find the element options on each iteration so that the element is found even after page refresh.

try:
    while True:
        browser.get('URL')
        options = browser.find_elements_by_xpath('//*[@id="event"]/tbody/tr')
        for i in range(len(options)):
            options = browser.find_elements_by_xpath('//*[@id="event"]/tbody/tr')
            options[i].find_element_by_xpath('./td[9]').click()
            time.sleep(2.5)
            break
except (ElementNotVisibleException, WebDriverException, NoSuchElementException):
        pass
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement