Python 异常处理

Python 异常处理,python,exception,error-handling,while-loop,Python,Exception,Error Handling,While Loop,我正在尝试编写一个while循环来检查自动登录期间的错误。我希望它通过登录运行,如果有错误,然后刷新页面并重新开始。当我尝试使用异常InvalidSelectorException:时,我得到一个未定义的错误。有没有其他方法可以让我写这个?或者我应该使用另一个例外 while True: try: loginButton = browser.find_element_by_xpath('//*[@id="global-header"]/div[2]/ul/li[2]/a'

我正在尝试编写一个while循环来检查自动登录期间的错误。我希望它通过登录运行,如果有错误,然后刷新页面并重新开始。当我尝试使用异常
InvalidSelectorException:
时,我得到一个未定义的错误。有没有其他方法可以让我写这个?或者我应该使用另一个例外

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()

无一例外的例外条款修复了它

您应该显示导入语句用于帮助人们解决此问题的内容。我的猜测是您没有正确导入“InvalidSelectorException”。@RalphCaraveo我修复了它。它只是没有处理异常语句。当我给它没有例外的时候,它工作得很好。你应该展示你的导入语句是如何帮助人们解决这个问题的。我的猜测是您没有正确导入“InvalidSelectorException”。@RalphCaraveo我修复了它。它只是没有处理异常语句。当我毫不例外地给它工作良好。
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.