How do I press a button with parent class name?

Issue

I’m trying to make a bot that can press a button for me with parent class name.

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(1)
    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.uc-btn#uc-btn-accept-banner"))).click()
    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Vælg størrelse']"))).click()
    browser.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//label[starts-with(@for, 'size-picker')]//span[text()='51.5']"))))
    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[starts-with(@for, 'size-picker')]//span[text()='51.5']"))).click()
    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Læg i indkøbskurv']"))).click()
    WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'a[title="Indkøbskurv"]'))).click()
    WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'z-1-button z-coast-base-primary-accessible z-coast-base__totals-tile__button-checkout z-1-button--primary z-1-button--button'))).click()

What I have tried:

WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'z-1-button z-coast-base-primary-accessible z-coast-base__totals-tile__button-checkout z-1-button--primary z-1-button--button'))).click()

The Error:

selenium.common.exceptions.TimeoutException: Message:

Website: Link

On the website on the right side there should be a orange button that says "Videre til kassen" that button is the button I try to press.

Context: Link

Solution

I am assuming that you are trying to click on this button:
enter image description here

If yes, then you may use the below code to click it:

WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.XPATH, "//*[@class='z-coast-base__totals-tile']//*[@class='z-1-button__content']"))).click()

If you essentially want a CSS SELECTOR instead of an xpath, you may replace it with this:

WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[class='z-coast-base__totals-tile'] div[class='z-1-button__content']"))).click()

OR even this:

WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".z-coast-base__totals-tile .z-1-button__content"))).click()

Also, I see the Webelement locator you are using for addToCartBtn is poor. It is absolute and very unreliable. Try to make them as relative as possible. I am referring to this one: /html/body/div[4]/div/div[2]/div/div/div[2]/div[1]/x-wrapper-re-1-6/div/div[4]/button

Answered By – Anand Gautam

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published