Home > front end >  How do i use long CSS_SELECTOR with WebDriverWait?
How do i use long CSS_SELECTOR with WebDriverWait?

Time:01-02

Context: Link

—————————————————————

I want to press that little back in the top right corner. I have copyed the selector from the little bag. And now I want it to press on it in my script.

The selector code look likes this:

#z-navicat-header-root > header > div:nth-child(3) > div > div > div > div.mrHm0Z.C3wGFf > div > div > div > div.z-navicat-header_topRow > div.z-navicat-header_langNavTools > div > div.z-navicat-header_navToolItem.z-navicat-header_navToolItem-bag > a > div > svg

What i haved tried:

WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.z-navicat#z-navicat-header-root > header > div:nth-child(3) > div > div > div > div.mrHm0Z.C3wGFf > div > div > div > div.z-navicat-header_topRow > div.z-navicat-header_langNavTools > div > div.z-navicat-header_navToolItem.z-navicat-header_navToolItem-bag > a > div > svg"))).click()

The 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)
    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.element_to_be_clickable((By.CSS_SELECTOR, "button.z-navicat#z-navicat-header-root > header > div:nth-child(3) > div > div > div > div.mrHm0Z.C3wGFf > div > div > div > div.z-navicat-header_topRow > div.z-navicat-header_langNavTools > div > div.z-navicat-header_navToolItem.z-navicat-header_navToolItem-bag > a > div > svg"))).click()

Right now it doesn’t work, but If there are any wise people around selenium and chromedriver out there then you are very welcome to leave a comment on how I fix this problem

Website: Link

CodePudding user response:

There is no need to use the absolute path just call the element, in same way you did with the "accept-banner" by its title attribute:

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'a[title="Indkøbskurv"]'))).click()

or its href attribute:

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'a[href="/cart/"]'))).click()

or by its parent class name:

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '.z-navicat-header_navToolItem-bag a'))).click()
  • Related