如何按下父级 class 名称的按钮?

How do I press a button with parent class name?

我正在尝试制作一个可以使用父 class 名称为我按下按钮的机器人。

我的代码:

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()

我尝试过的:

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()

错误:

selenium.common.exceptions.TimeoutException: Message:

网站:Link

在右侧的网站上应该有一个橙色按钮,上面写着“Videre til kassen”,该按钮是我尝试按下的按钮。

上下文:

我假设您正在尝试单击此按钮:

如果是,那么您可以使用下面的代码点击它:

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

如果您本质上想要一个 CSS 选择器而不是 xpath,您可以将其替换为:

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()

或者甚至这样:

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

此外,我发现您用于 addToCartBtn 的 Webelement 定位器很差。这是绝对的,非常不可靠。尝试使它们尽可能相关。我指的是这个:/html/body/div[4]/div/div[2]/div/div/div[2]/div[1]/x-wrapper-re-1-6/div/div[4]/button

我用 SeleniumBase 的解决方案创建了一个简化的脚本。 pip install seleniumbase,然后将该代码放入 Python 文件,然后 运行 使用 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")')