Amazon web scraping (hidden element) after redirect to cart page (python, selenium)

Question:

How can I get the hidden information from cart when I update the items in cart to 999(itrack method). The class of the span is a-size-base. I use python and selenium(I attached the program)
So my program will open a product page link, click on add to cart, redirect to cart page and now I want to extract the information in the span with class a-size-base. You can see that span tag if you update the items in the cart to 999 and click enter. It will appear an alert span. I want to extract that information.
I will appreciate any help.

from selenium import webdriver
from time import sleep

url = "https://www.amazon.com/gp/product/B0118QC1BA/ref=s9_acsd_cdeal_hd_bw_bFmNr_c_x_w?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=merchandised-search-5&pf_rd_r=AZJF41VDFJMPA4XY6D95&pf_rd_t=101&pf_rd_p=32a36b64-58af-5269-b81a-c1030ee0250c&pf_rd_i=3760911"

driver = webdriver.Chrome(executable_path="C:\UsersAndreiDownloadschromedriver_win32chromedriver.exe")
driver.get(url)

sleep(3)

driver.find_element_by_xpath('//*[@id="submit.add-to-cart"]/span/input').click()

sleep(3)

#driver.find_element_by_xpath('//*[@id="smartShelfAddToCartContinue"]/span/input')
driver.execute_script("document.getElementById('smartShelfAddToCartNative').click()")

sleep(3)

items_cart = driver.find_element_by_xpath('//div[@class="a-alert-content"]/span')

print driver.execute_script("return arguments[0].innerHTML",items_cart)
Asked By: ryy77

||

Answers:

Here is how I changed the number of items in the cart

# update the quantity 
from selenium.webdriver.common.keys import Keys
driver.find_element_by_xpath('//*[@id="a-autoid-0-announce"]/span[2]').click()
driver.find_element_by_xpath('//*[@id="dropdown1_9"]').click()
quantity_xpath = '//*[@id="activeCartViewForm"]/div[2]/div/div[4]/div/div[3]/div/div/input'
quantity_el = driver.find_element_by_xpath(quantity_xpath)
quantity_el.send_keys("999" + Keys.ENTER)

Now the quantity is updated, the alert message should appear. It seems while using your xpath you for item_cart you are picking an alert for delivery status so you have to narrow down you xpath string to be more specific.

items_cart = driver.find_element_by_xpath('//*[@class="sc-list-item-content"]//*[@class="a-alert-content"]/span')
print items_cart.text
Answered By: hashmuke
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.