I am trying to automate making purchases on nike.com with python selenium, but my program always fails because of a common error - unable to locate element. Initially, I was trying to use <driver>.find_element_by_xpath(<xpath>)
, but sometimes the elements were not available in time for them to be found, so I decided to use WebdriverWait:
try:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//*[@id='placeorder']/div/div[1]/button")))
#(driver.find_element("//button[text() = 'Place Order']"))
print("found the button!")
except:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//*[@id='payment']/div/div[1]/div[2]/div[5]/button")))
#(driver.find_element_by_xpath("//button[text() = 'Continue To Order Review']"))
print("found the alternate button")
I'm using a try/except block because the button that I need to complete the purchase is not always the same. In one case, it could be a button saying 'place order', or it could be a differently shaped button saying 'continue to order review.' Here they are:
[
Unfortunately, in these cases, the button is still not clicked (or found), and the script throws a TimeoutException. How can I get around this type of error? I've been looking through similar people's errors, but I can't seem to find a solution that works.
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//button[text()='Place Order']|//button[text()='Continue To Order Review']")))
You can use | as an or to find one or the other.
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.