Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to select button with only class values in Selenium Python?

Is it possible to select button with only class values in Selenium Python? The html is as follows:

<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)

Any idea to select that button?

like image 346
2964502 Avatar asked Sep 13 '25 22:09

2964502


1 Answers

Yes, it is possible.

Use find_element_by_class_name (or find_elements_by_class_name to get multiple matched elements):

browser.find_element_by_class_name('secondary')

You can also use find_element_by_css_selector (or ..s variant):

browser.find_element_by_css_selector('.secondary')

Alternatively you can also use find_element_by_xpath, but it requires a verbose xpath expression to be precise:

xpath = "descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' secondary ')]"
browser.find_element_by_xpath(xpath)
like image 181
falsetru Avatar answered Sep 15 '25 10:09

falsetru