'How do I tell selenium to add to basket if item is 'in stock'?

I have the following code that prints 'in stock' if the item is in stock:

from selenium import webdriver

from selenium.webdriver.chrome.options import Options

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.common.by import By

from selenium.webdriver.support import expected_conditions as EC


options=Options()
driver=webdriver.Chrome(options=options)
    
#Directing to site
driver.get("https://www.amazon.co.uk/Nintendo-Switch-OLED-Model-Neon/dp/B098TNW7NM/ref=sr_1_3?keywords=Nintendo+Switch&qid=1651147043&sr=8-3");
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/span/form/div[3]/span[1]/span/input"))).click()



condition= print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@id='availability']//span[contains(text(),'In stock.')]"))).get_attribute("innerHTML"))
if condition == 'In stock.':
    
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[3]/div[6]/div[4]/div[1]/div[2]/div/div/div/div/div/div/form/div/div/div/div/div[3]/div/div[14]/div[1]/span/span/span/input"))).click()

else:
    pass

How do I tell selenium to 'add to basket' if the item is 'in stock'?

I want to:

  1. go to this url: https://www.amazon.co.uk/Nintendo-Switch-OLED-Model-Neon/dp/B098TNW7NM/ref=sr_1_3?keywords=Nintendo+Switch&qid=1651147043&sr=8-3

  2. check if the text on the right side says 'In stock.'.

  3. If it is in stock, I'd like to add it to basket.

I got the xpath by going inspect->copy->copy full x path.



Solution 1:[1]

You should ideally use relative XPath.

We are using presence_of_all_elements_located which will return a list of web elements if found, if not then it will be an empty list, so in_stock is a list of web elements. Now, len() is a method that will give us the output of how many web elements are present in the list. therefore, the logic is something like, if the list has anything then it must be In stock -> Now add to basket, else it must be <0 or 0 in that case "In stock" won't be present and we won't add anything to our basket.

The code below goes to the URL, look for "In stock" text if it is present then "Add to basket" if not simply tear down the bot.

Code:

driver.maximize_window()
wait = WebDriverWait(driver, 30)

driver.get("https://www.amazon.co.uk/Nintendo-Switch-OLED-Model-Neon/dp/B098TNW7NM/ref=sr_1_3?keywords=Nintendo+Switch&qid=1651147043&sr=8-3")
try:
    wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/span/form/div[3]/span[1]/span/input"))).click()
    print("clicked on accept cookies successfully")
except:
    pass

in_stock = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//div[@id='availability']//span[contains(text(),' In stock. ')]")))
try:
    if len(in_stock) > 0:
        print("In stock must be present.")
        wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#add-to-cart-button"))).click()
        print("Clicked on add to cart button")
    else:
        print("In stock must be not present")
except:
    print("Something went wrong")
    pass

Imports:

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

Solution 2:[2]

assuming the last line of your code returns 'in stock'

condition=WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@id='availability']//span[contains(text(),'In stock.')]"))).get_attribute("innerHTML")

if condition == 'in stock':
    # logic to add to cart
else:
    pass

for the logic on adding an item/interacting with buttons look into this : https://pythonspot.com/selenium-click-button/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 sensualbumfuzzle