How to find an element using xpath?

211 Views Asked by At

I am new to coding and I am trying to create a checkout bot. I am using selenium to help me do this. So far my code works up until the checkout I can't seem to get my bot to click the checkout button.

This is what I get when I inspect the checkout button.

   <a href="https://www.bestbuy.ca/identity/global/signin?redirectUrl=https%3A%2F%2Fwww.bestbuy.ca%2Fcheckout%2F%3Fqit%3D1%23%2Fen-ca%2Fshipping%2FON%2FM4W&lang=en-CA&contextId=checkout" class="button_E6SE9 primary_1oCqK continueToCheckout_3Dgpe regular_1jnnf" data-automation="continue-to-checkout"> == $0

I have tried

find_element_by_class_name("continueToCheckout_3Dgpe")
find_element_by_xpath( '//*[@class="continueToCheckout_3Dgpe"]' ).click()
findElement(By.cssSelector("a[href*='https://www.bestbuy.ca/identity/global/signin?redirectUrl=https%3A%2F%2Fwww.bestbuy.ca%2Fcheckout%2F%3Fqit%3D1%23%2Fen-ca%2Fshipping%2FON%2FM4W&lang=en-CA&contextId=checkout']")).click();
find_element_by_partial_link_text('https://www.bestbuy.ca/identity/global/signin?redirectUrl=https%3A%2F%2Fwww.bestbuy.ca%2Fcheckout%2F%3Fqit%3D1%23%2Fen-ca%2Fshipping%2FON%2FM4W&lang=en-CA&contextId=checkout')
find_element_by_xpath('//a[@href="https://www.bestbuy.ca/identity/global/signin?redirectUrl=https%3A%2F%2Fwww.bestbuy.ca%2Fcheckout%2F%3Fqit%3D1%23%2Fen-ca%2Fshipping%2FON%2FM4W&lang=en-CA&contextId=checkout"]')
find_element(By.xpath("//a[@href='https://www.bestbuy.ca/identity/global/signin?redirectUrl=https%3A%2F%2Fwww.bestbuy.ca%2Fcheckout%2F%3Fqit%3D1%23%2Fen-ca%2Fshipping%2FON%2FM4W&lang=en-CA&contextId=checkout']"))

None seems to work and doesn't click the checkout button. If any idea on how to fix this it would be helpful! Thank you :)

1

There are 1 best solutions below

2
On
driver.find_element_by_xpath("//a[@data-automation='continue-to-checkout']").click()

Would click the a tag with the attribute continue to checkout. Now you might need to wait if you switch to that page or etc.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//a[@data-automation='continue-to-checkout']"))).click()