【发布时间】:2022-01-22 20:11:41
【问题描述】:
使用 python 中的 selenium,如果它包含一些单词并且找不到任何脚本必须退出,我想单击 html div 容器。
使用下面的代码,如果有一个 div 包含来自text 列表的单词,但我如何退出没有找到单词的地方?使用下面的代码,它会执行order.click,因为这是在 for 循环之外。我只想执行order.click(),如果找到的话,继续使用脚本的其余部分break
text = ["Dog", "Cat", "Bird"]
for word in text:
try:
order = WebDriverWait(driver,5).until(EC.presence_of_element_located((By.XPATH, "//div/p[contains(text(),'{}')]".format(word))))
if order != None:
print(f"found div with word: {word}")
break
except:
print(f"did NOT found div with word: {word}")
order.click()
# and more commands after this....
【问题讨论】:
标签: python python-3.x for-loop selenium-webdriver