我正在尝试在网页上搜索链接文本,并且如果找不到该项目,请尝试使程序响应无错误,然后移至其他项目。
from selenium.common.exceptions import NoSuchElementException
hasElementQ = True
while True:
try:
elem = browser.find_element_by_link_text('pant')
break
except NoSuchElementException:
print ('it doesnt exist')
这是我收到的错误:
except NoSuchElementException:
^
SyntaxError: invalid syntax
最佳答案
您的except
必须与try
处于相同的缩进级别
from selenium.common.exceptions import NoSuchElementException
hasElementQ = True
while True:
try:
elem = browser.find_element_by_link_text('pant')
break
except NoSuchElementException:
print ('it doesnt exist')
关于python - 在 Selenium 中找不到项目时如何引发错误并转到其他功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53235877/