概述
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 selenium循环判断元素是否存在_检查元素是否存在python selenium所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复