How to click buttons on amazon with selenium?

Question:

enter image description here

I’m trying to click buttons on amazon with selenium (python) but it won’t work. It says the elements don’t exist. I tried to google it but only found outdated solutions. I’ve tried Xpaths,ID,full XPATH on pretty much all input fields with no success. I’ve used selenium before and it works flawlessly but on amazon in particular it can’t find the elements. For example the place order button (Je bestelling plaatsen in my language) can’t be found nor clicked.

driver.find_element(By.XPATH, '//*[@id="turbo-checkout-pyo-button"]/span/input')

driver.find_element(By.ID, 'turbo-checkout-pyo-button')
driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/div/div/div/div[2]/div/form/div/span/span/span/input')

How do I know which elements are clickable and direct to the intended page? If there is a way to tell please let me know. Thanks in advance

Asked By: koeliga

||

Answers:

I’m not a selenium master but you can use the find_element_by_xpath function that returns an element object that can be clicked so:

element = driver.find_element_by_xpath('/html/body/div[4]/div[1]/div/div/div/div[2]/div/form/div/span/span/span/input')
element.click()
Answered By: MeatBALL

The problem was that the button was located in an iframe. Switching to the iframe and then pressing the button does the job. Found out through this question

driver.switch_to.frame("turbo-checkout-iframe")
directButton = driver.find_element(By.CSS_SELECTOR, '#turbo-checkout-pyo-button')
directButton.click()
Answered By: koeliga

Here at Zinc.com we have done Selenium integrations with Amazon but have run into many of the same challenges, so ended up rebuilding a custom browser based solution that can effectively scrape product/pricing data and place orders at scale via API calls. While this method works very well for us and our customers, it still takes a lot of constant updating and engineering resources to maintain.

Answered By: Joey Thomas
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.