Python selenium错误处理
我有一个python selenium脚本,它通过这样一个循环运行Python selenium错误处理,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我有一个python selenium脚本,它通过这样一个循环运行 for i, refcode in enumerate(refcode_list): try: source_checkrefcode() except TimeoutException: pass csvWriter.writerow([refcode, 'error', timestamp]) 如果源代码检查过程中出现问题,则脚本将崩溃并出现错误 如何将错误
for i, refcode in enumerate(refcode_list):
try:
source_checkrefcode()
except TimeoutException:
pass
csvWriter.writerow([refcode, 'error', timestamp])
如果源代码检查过程中出现问题,则脚本将崩溃并出现错误
如何将错误处理添加到此循环中,使其只移动到下一个项目而不是崩溃?您可以添加检查异常消息,下面是示例代码供您理解
for i, refcode in enumerate(refcode_list):
try:
source_checkrefcode()
except Exception as e:
if 'particular message' in str(e):
# Do the following
# if you don't want to stop for loop then just continue
continue
您可以添加检查异常消息,下面是示例代码供您理解
for i, refcode in enumerate(refcode_list):
try:
source_checkrefcode()
except Exception as e:
if 'particular message' in str(e):
# Do the following
# if you don't want to stop for loop then just continue
continue
我同意哈桑的回答。但是如果使用continue,则不会处理任何其他代码块
for i, refcode in enumerate(refcode_list):
try:
source_checkrefcode()
except Exception as e:
if 'particular message' in str(e):
# Do the following
# if you don't want to stop for loop then just continue
continue
# another code of block will skip. Use pass or continue as per your requirement.
您必须理解“通过”和“继续”之间的区别
我同意哈桑的回答。但是如果使用continue,则不会处理任何其他代码块
for i, refcode in enumerate(refcode_list):
try:
source_checkrefcode()
except Exception as e:
if 'particular message' in str(e):
# Do the following
# if you don't want to stop for loop then just continue
continue
# another code of block will skip. Use pass or continue as per your requirement.
您必须理解“通过”和“继续”之间的区别
我的原始代码查找TimeoutException,但您的示例使用了Exception。这段新代码还会捕获TimeoutException吗?是的,实际上Exception将捕获所有类型的异常。然后,您可以在其字符串消息上添加检查。例如如果str(e)中的'TimeountException':执行此操作如果str(e)中的'other Exception':执行此操作等等,我的原始代码会查找TimeoutException,但您的示例使用Exception。这段新代码还会捕获TimeoutException吗?是的,实际上Exception将捕获所有类型的异常。然后,您可以在其字符串消息上添加检查。例如如果str(e)中的'TimeountException',则执行此操作;如果str(e)中的'other Exception',则执行此操作,依此类推