I'm trying to locate element by
element=driver.find_element_by_partial_link_text("text")
in Python selenium and the element does not always exist. Is there a quick line to check if it exists and get NULL or FALSE in place of the error message when it doesn't exist?
解决方案
You can implement try/except block as below to check whether element present or not:
from selenium.common.exceptions import NoSuchElementException
try:
element=driver.find_element_by_partial_link_text("text")
except NoSuchElementException:
print("No element found")
or check the same with one of find_elements_...() methods. It should return you empty list or list of elements matched by passed selector, but no exception in case no elements found:
elements=driver.find_elements_by_partial_link_text("text")
if not elements:
print("No element found")
else:
element = elements[0]
最后
以上就是等待音响最近收集整理的关于python selenium循环判断元素是否存在_检查元素是否存在python selenium的全部内容,更多相关python内容请搜索靠谱客的其他文章。
发表评论 取消回复