我是靠谱客的博主 等待音响,这篇文章主要介绍python selenium循环判断元素是否存在_检查元素是否存在python selenium,现在分享给大家,希望可以做个参考。

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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(98)

评论列表共有 0 条评论

立即
投稿
返回
顶部