0

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

Sadra Saderi
  • 52
  • 1
  • 8

2 Answers2

1

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

Anand Gautam
  • 2,018
  • 1
  • 3
  • 8
  • Hey Anand Gautam, Thanks for the answer is working very well, I'm using solutions number 3 and its working. But one question how did you find out to this solutions? So I can learn a little bit. – Robert Tacchini Jan 10 '22 at 18:17
  • @Robert, one has to learn xpath and css selector strategies (precisely, web element locator strategies). Search them on Google and you would find a plethora of information, and then practice on websites' DOM. Thank you – Anand Gautam Jan 12 '22 at 06:34
0

I created a simplified script with the solution using SeleniumBase. pip install seleniumbase, then drop that code into a Python file, and then run the file using pytest.

from seleniumbase import BaseCase


class MyTestClass(BaseCase):
    def test_base(self):
        self.open("https://www.zalando.dk/jordan-air-jordan-1-mid-sneakers-high-joc12n001-a18.html")
        self.click("button#uc-btn-accept-banner")
        self.click("button#picker-trigger")
        self.click('label[for="size-picker-JOC12N001-A180170000"] span span')
        self.click('button[aria-label="Læg i indkøbskurv"]')
        self.click('a[title="Indkøbskurv"] svg')
        self.click('button:contains("Videre til kassen")')
Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
  • Hi, Thanks for the reply. I think I go with the second answer as it's a little more inside what I'm up to now, also with the code. – Robert Tacchini Jan 10 '22 at 18:22