是否有相当于 BeautifulSoup 的 has_attr() 函数的 Selenium?

Is there a Selenium equivalent to BeautifulSoup's has_attr() function?

例如,如果需要检查一个元素是否具有 class 属性(在 bs4 中它是 element.has_attr("class") 的位置),这在 Selenium 中如何完成?

您可以使用 .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 =(