简体   繁体   中英

Exception Handling

I'm trying to write a while loop to check for errors during an automated login. I want it to run through the login and if there is an error then refresh the page and start again. When I try to use the exception InvalidSelectorException: I get an undefined error. Is there another way I should write this? Or a different exception I should use?

while True:
    try:
        loginButton = browser.find_element_by_xpath('//*[@id="global-header"]/div[2]/ul/li[2]/a')
        loginButton.click()
        time.sleep(3) 

        iframe = browser.switch_to.frame('disneyid-iframe') 
        Username = browser.find_element_by_xpath('//*[@id="did-ui"]/div/div/section/section/form/section/')
        Username.send_keys(usernameStr)
        time.sleep(3)


        password = browser.find_element_by_xpath('//*[@id="did-ui"]/div/div/section/section/form/section/div[2]/div/label/span[2]/input')
        password.send_keys(passwordStr)
        time.sleep(3)


        nextButton = browser.find_element_by_xpath('//*[@id="did-ui"]/div/div/section/section/form/section/div[3]/button[2]')
        nextButton.click()
        break 
    except:
        browser.refresh()

the exception clause with no exception fixed it.

You can also use the except statement with no exceptions defined as follows −

try:
   You do your operations here;
   ......................
except:
   If there is any exception, then execute this block.
   ......................
else:
   If there is no exception then execute this block. 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM