Search code examples
seleniumselenium-webdriverselenium-chromedriverwebdriverwait

How to select the right size of a product? Selenium - ChromeDriver


My Code:

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

browser = webdriver.Chrome('C:/Users/rober/OneDrive/Skrivebord/bot/chromedriver')

# Graffik kort
browser.get("https://www.zalando.dk/jordan-air-jordan-1-mid-sneakers-high-joc12n001-a18.html")

buyButton = False

while buyButton is False:

    try:
        
        addToCartBtn = addButton = browser.find_element_by_xpath('/html/body/div[4]/div/div[2]/div/div/div[2]/div[1]/x-wrapper-re-1-6/div/div[4]/button')

        print("Varen er udsolgt")

        time.sleep(1)
        browser.refresh()

    except:
        addToCartBtn = addButton = browser.find_element_by_xpath('//*[@id="picker-trigger"]')

        print("Varen er på Lager")
        buyButton = True

while buyButton is True:
    time.sleep(3)
    accept = browser.find_element_by_id('uc-btn-accept-banner')
    browser.execute_script("arguments[0].click();", accept) 
    element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="picker-trigger"]')))
    element.click(); 

My Error:

Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="uc-btn-accept-banner"]"}

The link to the Website

My problem is that i want to get the size 51.5 in eu sizes but i don't know the code to select that size with the CLASS of the size.

If some one know a solution, just make a comment because i really need some help to find the right solution.


Solution

  • To select the size as 51.5 you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategies:

    • Code Block:

      driver.get("https://www.zalando.dk/jordan-air-jordan-1-mid-sneakers-high-joc12n001-a18.html")
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.uc-btn#uc-btn-accept-banner"))).click()
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Vælg størrelse']"))).click()
      driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//label[starts-with(@for, 'size-picker')]//span[text()='51.5']"))))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[starts-with(@for, 'size-picker')]//span[text()='51.5']"))).click()
      
    • Browser Snapshot:

    51.5

    • Note : You have to add the following imports :

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