有没有Selenium相当于BeautifulSoup的has_attr()功能?

2024-03-29 15:52:44 发布

您现在位置:Python中文网/ 问答频道 /正文

例如,如果需要检查一个元素是否有class属性(在bs4中它是element.has_attr("class")),那么如何在Selenium中做到这一点呢?你知道吗


Tags: 元素属性seleniumelementclassattrhasbs4
1条回答
网友
1楼 · 发布于 2024-03-29 15:52:44

您可以使用.get_attribute()方法并检查结果是否是None,如下所示:

x = element.get_attribute('class')
if x is not None:
    # then it has the attribute!
else:
    # it doesn't have the attribute =(

相关问题 更多 >