bbfl
bbfl

Reputation: 365

Continue test after element is not available in while loop

I have a web page where I have 5 same class buttons and there are 500 and more pages with the same buttons on every page and I made a while loop to click on all of them. But eventually at the end where buttons are no longer available I receive exit error code. How can I make it print for example "No more buttons available" and finish the test.

res = 1
while res < 10000:
    buttons = driver.find_elements_by_xpath('//*[@class="btn default check check green markAsChecked"]')
    buttons[0].send_keys('\n')
    buttons = driver.find_elements_by_xpath('//*[@class="btn default check check green markAsChecked"]')
    buttons[1].send_keys('\n')
    buttons = driver.find_elements_by_xpath('//*[@class="btn default check check green markAsChecked"]')
    buttons[2].send_keys('\n')
    buttons = driver.find_elements_by_xpath('//*[@class="btn default check check green markAsChecked"]')
    buttons[3].send_keys('\n')
    buttons = driver.find_elements_by_xpath('//*[@class="btn default check check green markAsChecked"]')
    buttons[4].send_keys('\n')
    driver.refresh()
    res = res + 1

Upvotes: 0

Views: 517

Answers (2)

KunduK
KunduK

Reputation: 33384

Try use if..else condition to check the length of button elements.If the length of the button more than zero then iterate and do some operation else print No more buttons available and break.

res = 1
while res < 10000:
    buttons = driver.find_elements_by_xpath('//*[@class="btn default check check green markAsChecked"]')
    if len(buttons)>0:
      for idx in range(len(buttons)):
          buttons = driver.find_elements_by_xpath('//*[@class="btn default check check green markAsChecked"]')
          buttons[idx].send_keys('\n')

      driver.refresh()
      res = res + 1
    else:
        print("No more buttons available")
        break

Upvotes: 2

CEH
CEH

Reputation: 5909

I would refactor your loop to be more efficient and introduce a index variable for your buttons list:

from selenium.common.exceptions import NoSuchElementException


res = 1

while res < 10000:

    # find buttons list
    buttons = driver.find_elements_by_xpath('//*[@class="btn default check check green markAsChecked"]')

    # iterate and send keys to buttons[0], buttons[1], etc..
    for i in range(0,4):
        try:
            buttons[i].send_keys('\n')

            # find buttons again, in case buttons element list goes stale
            buttons = driver.find_elements_by_xpath('//*[@class="btn default check check green markAsChecked"]')

        except NoSuchElementException: # case: button does not exist
            break

    driver.refresh()
    res = res + 1

Now instead of calling buttons[0].send_keys, buttons[1].send_keys, all the way to buttons[4], we are using a for loop from 0 to 4 to accomplish this, performing button[i].send_keys.

Inside the for loop, we re-run the call for buttons = driver.find_elements... so that we can refresh the button elements in the list. This is to handle a potential StaleElementReferenceException that may appear as you iterate through the buttons.

We also wrap button[i].send_keys in a try / except block to catch the NoSuchElementException to handle the case where buttons do not exist on the page. The code will break out of the for loop once buttons are no longer found, and continue executing in the while loop.

Something to note -- this will not handle the case where there are more or less than exactly 5 buttons on the page. If the number of buttons is subject to change, we want to use for button in buttons: instead of for i in range(0,4). If this is the case, please let me know so that I can refactor my code.

Upvotes: 1

Related Questions