gpt4 book ai didi

python - 异常处理

转载 作者:行者123 更新时间:2023-12-03 07:58:31 24 4
gpt4 key购买 nike

我试图编写一个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.

关于python - 异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43617092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com