繁体   English   中英

异常处理

[英]Exception Handling

我正在尝试编写一个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()

异常子句,没有异常修复它。

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. 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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