I am currently playing around with Selenium trying to learn it by doing and have encountered this issue; occasionally a button on the website appears with a different XPATH, so I wanted to try and create a workaround. This is what I am trying to use:
if bool(wd.find_element_by_xpath('//*[@id="basketPageApp"]/div/div[2]/div/div/div[1]/div/div[2]/a')) == TRUE:
button = wd.find_element_by_xpath('//*[@id="basketPageApp"]/div/div[2]/div/div/div[1]/div/div[2]/a')
else:
button = wd.find_element_by_xpath('//*[@id="guestCheckout"]/div/div/div[2]/section/div[2]/div[1]/div')
I'm sure I can't use an if statement in this manner but I hope this gets the idea of what I am trying to achieve across.