Home > Enterprise >  Retry Selenium Click
Retry Selenium Click

Time:01-28

driver.find_element(By.XPATH, "//input[@name = 'SelectAll' and @value='Move All >>>']").click()
driver.find_element(By.NAME, "Submit1").click()

For background, this is part of a script that goes to a website, completes a few actions/prompts, and pulls a CSV file. The code above works most of the time but sometimes gives an error of,

UnexpectedAlertPresentException: unexpected alert open: {Alert text : No contracts have been selected.}
  (Session info: headless chrome=97.0.4692.99)

This is from a page where you have to move list items over from one dialogue box to another. You receive this error when nothing has been pulled over. Also as a note, I do have an implicitly_wait(100) before this code.

I am trying to get the above code to run through a loop where it will keep repeating/retrying the code until there is a success.

CodePudding user response:

If I clearly understnd what goes there:
You want to continue performing the 2 click actions you mentioned until no alert appeared.
I.e. in case alert appearing - close the alert and perform the clicks again.
In case no alert appeared - no more need to click these 2 elements, continue further with the next code.
If so you can do something like this:

while True:
    driver.find_element(By.XPATH, "//input[@name = 'SelectAll' and @value='Move All >>>']").click()
    driver.find_element(By.NAME, "Submit1").click()
    try:
        alert = driver.switch_to.alert
        alert.accept()
    except:
        break
  •  Tags:  
  • Related