是否可以在 Selenium Python 中选择仅具有类值的按钮? html如下:
<button class="secondary option-action ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false"
from selenium import webdriver
browser = webdriver.Firefox()
browser.get(any_url)
知道选择那个按钮吗?
请您参考如下方法:
是的,这是可能的。
使用find_element_by_class_name (或 find_elements_by_class_name 获取多个匹配元素):
browser.find_element_by_class_name('secondary')
您还可以使用 find_element_by_css_selector (或 ..s 变体):
browser.find_element_by_css_selector('.secondary')
或者,您也可以使用 find_element_by_xpath,但它需要一个冗长的 xpath 表达式才能精确:
xpath = "descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' secondary ')]"
browser.find_element_by_xpath(xpath)
