I'm using Selenium to gather the difference in total price of several items (including taxes and shipping cost) from a Shopify based website. I've so far successfully been able to add items to my cart. This requires adding the selected items to the cart, and then going to the checkout page, updating city, zip, and state, and finally ending on the finalize page to capture the total.
Initially, my code was successfully running well enough to get me to the checkout page, but was unable to locate elements to send the keys I needed. While working on solving that, something changed and suddenly stopped locating the checkout button at all.
From what I have been looking into, it feels the issue might be that need to switch frames - indeed when you click the cart button, a side bar pops up with the button needed to go to checkout. I've been expecting the html and can't for the life of me find these things within a frame tag. I've also been using time.sleep() to allow things time to load, as well as scroll to bring things into view and still, no luck.
Here's an example of what the website looks like after you select the cart:
you can see there is a pop up in the way
Here is what my code looks like:
url = 'https://americanbarbell.com/products/american-barbell-cast-kettlebell'
index_list = [('4KG',1),('12KG,4)]
smallerlist = ['4KG', '12KG']
path = "C:\Program Files (x86)\msedgedriver.exe"
driver = webdriver.Edge(path)
driver.get(url)
time.sleep(1)
driver.refresh() ###I have this here because a pop up comes up when the webdriver opens the page, and this was a workaround to actually close it
time.sleep(1)
driver.execute_script("window.scrollTo(0, 550)")
for item in smallerlist:
for tup in index_list:
if item == tup[0]:
n = tup[-1]
time.sleep(2)
driver.find_element(By.XPATH, '//*[@id="product_form_377205240"]/div[2]/div/div/div').click()
time.sleep(2)
driver.find_element(By.XPATH, f'//*[@id=\"377205240-weight\"]/li[{n}]/div').click()
time.sleep(1)
add_cart = driver.find_element(By.XPATH, '//*[@id="product_form_377205240"]/div[3]/button')
driver.execute_script("arguments[0].click();", add_cart)
time.sleep(3)
driver.refresh()
checkout = driver.find_element(By.XPATH,'/html/body/div[2]/div[5]/div[3]/div/form/div[2]/div[2]/div[3]/div[2]').click()
and I get the error:
Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[2]/div[5]/div[3]/div/form/div[2]/div[2]/div[3]/div[2]"}
I've also tried the XPATH:
/html/body/div[3]/div[5]/div[3]/div/form/div[2]/div[2]/div[3]/div[2]/button
Which returns the same error
I've tried using the x path of the pop up window to close it as well, Selenium also failed to locate the element as well.
Frustratingly the code was working just fine, and then out of nowhere just stopped. I'm guessing that my code was flimsy to begin with and I was just lucky. I've been stuck looking for a solution for far too long. I'm thinking that maybe it's within a frame I need to switch to? Do I need to wait longer? Is the element hidden by the pop up?
CodePudding user response:
Looks like the position of the element is not stable. You need to search for the element more precisely.
Replace checkout = driver.find_element(By.XPATH,'/html/body/div[2]/div[5]/div[3]/div/form/div[2]/div[2]/div[3]/div[2]').click()
by checkout = driver.find_element(By.XPATH, '//button[@name="checkout"]').click()
It worked for me in Chrome, hopefully it will work in Edge