在使用driver.find_element()方法时,如果定位元素失败,会返回一个字典格式的错误提示。可以通过以下代码,将字典中的错误信息提取出来并转为字符串格式,便于输出和查看:
from appium.webdriver.common.exceptions import NoSuchElementException
try:
driver.find_element_by_id('element_id')
except NoSuchElementException as e:
error_info = e.msg
error_str = str(error_info)
print('定位元素失败:{}'.format(error_str))
在try块中使用driver.find_element_by_id()方法尝试定位元素,如果失败则抛出NoSuchElementException异常。在except块中捕获异常,并将异常信息的字典格式转换为字符串格式进行输出。