简体   繁体   中英

How to verify if a button is clickable with Selenium in Python when the class changes?

I'm trying to find out if a "Ship to Home" button is available and able to be clicked on an item in The Home Depot website. Currently, the only identifier that I find for the button is a class but when you go from store pickup to ship to home the two classes switch places for the button element. Two Elements Switch<\/a>

I don't know much about selenium but I'm assuming there has to be some type of way to loop a section of the HTML and find out if the element contains some text and if it does to click on that element.

XPATH could be quite flexible and allows to refer a lot of element node's data and also in advance it alows some logical conditions, etc. So I believe it's very powerful and flexible enough to produce some targeted and not fragile results.

clickable_button_xpath = "//a[@type='button'][contains(., 'Ship to Home') and contains(@class, '-unselected')]"
selected_button_xpath = "//a[@type='button'][contains(., 'Ship to Home') and contains(@class, '-selected')]"
unclickable_button_xpath = "//a[@type='button'][contains(., 'Ship to Home') and contains(@class, '-unclickable')]"

some_element = driver.find_element(By.XPATH, some_xpath)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM