【发布时间】:2020-03-26 11:53:53
【问题描述】:
如何使用try 函数重试代码?
想法是:
try:
interaction_type2 = driver.find_elements_by_xpath('web.page.interaction1')
except Error:
interaction_type2 = driver.find_elements_by_xpath('web.page.interaction2')
每次它应该添加到交互+1并循环它直到没有错误,所以尝试每次执行代码直到它成功。 我用循环尝试了它,但没有得到我想要的结果。
我目前的代码:
while True:
x = 'web.page.interaction-70"]'
try:
interaction_type2 = driver.find_elements_by_xpath(x) # next 136 then 202, 268
except Exception:
x = x.replace('70', '138')
interaction_type2 = driver.find_elements_by_xpath(x)
interaction_type2.click()
不知道如何在不写每一行的情况下更改每个错误。
【问题讨论】:
-
循环是要使用的东西。给我们看看你写的循环?
标签: python-3.x loops try-catch