selenium 判断元素是否有属性
Selenium 是一个用于自动化测试的工具,您可以使用它来判断一个 HTML 元素是否有特定的属性。
通过使用 Selenium 的 API,您可以使用 has_attribute() 函数来判断一个元素是否有特定的属性。例如,如果您想判断一个元素是否具有 "class" 属性,您可以使用以下代码:
element = driver.find_element_by_id("element_id") if element.has_attribute("class"): print("The element has a 'class' attribute.") else: print("The element does not have a 'class' attribute.") 复制代码
如果您希望根据元素的属性值来判断,则可以使用 get_attribute() 函数。例如,如果您想判断元素的 "class" 属性是否为 "example",则可以使用以下代码:
element = driver.find_element_by_id("element_id") if element.get_attribute("class") == "example": print("The 'class' attribute of the element is 'example'.") else: print("The 'class' attribute of the element is not 'example'.")