如何根据给定的文本单击范围

2024-06-01 12:39:11 发布

您现在位置:Python中文网/ 问答频道 /正文

考虑一下: https://www.lanebryant.com/lace-trim-overpiece/prd-358988#color/0000006400

单击“我的尺寸”,然后单击“开始”。您将看到一个包含年龄范围的下拉列表。我需要根据我已经拥有的价值来点击它们。考虑我的代码:

wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div/div/div/div[2]/form/tfc-accordion-group/div[2]/div/div[2]/span/div[2]/div//span[text()=" + " "+ df_temp.age + " " + "]"))).click()

df_temp['age']的值为16-18

但它没有点击并给出超时异常。你知道吗


Tags: httpsdivcomdfagewwwtempcolor
2条回答

希望这能对你有所帮助,每一步的细节请看代码注释。你知道吗

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

# Configure the webdriver and webdriver-wait instances
driver = webdriver.Chrome()
driver.maximize_window()
wait = WebDriverWait(driver, 10)


# Load the webpage
driver.get("https://www.lanebryant.com/lace-trim-overpiece/prd-358988#color/0000006400")
time.sleep(5)

# Open the popup
wait.until(EC.presence_of_element_located(
    (By.XPATH, '//*[@id="358988"]//td/a[@class="tfc-popup-click-open"]'))).click()
time.sleep(4)

# Find the popup iframe and make driver to focus the same
popup_iframe = wait.until(EC.visibility_of_element_located(
    (By.XPATH, '//div[contains(@aria-label, "True Fit")]/iframe')))
driver.switch_to.frame(popup_iframe) 
# To switch the focus back to the main content window,
# execute: driver.switch_to.default_content()

# Click the get-started button
wait.until(EC.presence_of_element_located(
    (By.XPATH, '//*[contains(@class, "tfc-cfg-nav-button")]/button'))).click()

# Find the age dropdown
age_dropdown = wait.until(EC.presence_of_element_located(
    (By.XPATH, '//*[@class="tfc-select-custom"]/button')))

# Let's say you want to select age group: 
# 35-44, so minAge here is 35
min_age = 35

# Activate the age dropdown and select and click the desired option
age_dropdown.click()
age_option = wait.until(
    EC.visibility_of_element_located(
        (By.XPATH, '//*[@class="tfc-select-custom"]//div[@class="tfc-select-body"]/div/span[contains(text(),%s)]' % (min_age))))
age_option.click()

time.sleep(20)

首先单击下拉列表以打开选项列表,然后使用//div[@value="ageRange.id" and contains(.,"16 - 18")]//div[@value="ageRange.id" and normalize-space(.)="16 - 18"]xpath单击项:

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[aria-label="Age"]'))).click()
wait.until(EC.visibility_of_element_located((By.XPATH, '//div[@value="ageRange.id" and contains(.,"16 - 18")]'))).click()

相关问题 更多 >