下面有下面的代码,可以从页面中获取注释。有时没有注释,因此出现了except代码触发器。它在执行except代码之前尝试尝试代码块大约5-10秒。是否有更快的方法来检查元素是否被找到?最好是:如果找不到元素,那么直接执行not代码。
try:
comments = WebDriverWait(driver, 20).until(
EC.presence_of_all_elements_located((By.XPATH, relativeXpathToReportComments)))
# some code to be executed if the elements is found
except:
print("could not find/get comments on comment page")
发布于 2021-06-03 11:09:04
如果您想立即检查元素的存在,只需使用以下内容:
if driver.find_elements_by_xpath('relativeXpathToReportComments'):
# some code to be executed if the elements is found
else:
print("could not find/get comments on comment page")
driver.find_elements
返回与传递的定位器匹配的元素列表。
如果存在这样的元素,则返回非空的已发现元素列表。否则,它返回一个在Python中被识别为False
的空列表。
find_elements
和find_element
正在寻找由ImplicitWait
定义的直到超时的元素,默认为0。
https://stackoverflow.com/questions/67820064
复制相似问题