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

Python 如何根据给定的文本单击范围,python,selenium,Python,Selenium,考虑这一点: 单击我的尺码,然后单击开始。您将看到一个包含年龄范围的下拉列表。我需要根据我已有的值单击这些。考虑我的代码: 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 + " " +

考虑这一点:

单击我的尺码,然后单击开始。您将看到一个包含年龄范围的下拉列表。我需要根据我已有的值单击这些。考虑我的代码:

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


但它不会单击并给出超时异常

首先单击下拉列表以打开选项列表,然后使用
//div[@value=“ageRange.id”和contains(,,“16-18”)]
//div[@value=“ageRange.id”并规范化空格(.)=“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()

我希望这将对您有所帮助,有关每个步骤的详细信息,请参阅代码注释

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)

只需打印xpath并粘贴到开发人员工具的web控制台上,检查它是否正确。最后一个文本可用的组件是
label
not
span
@Kaushik explaint这是我得到的xpath
/*[@id=“productDetail”]/section/div[1]/div/div[3]/div[2]/div/div/div[4]/div/ul/li[3]/标签
@Yun我在chrome的控制台上键入了它。它变红了。我怎么知道它是正确的还是不正确的?等到(EC.element可点击((By.XPATH,//div[@value=“ageRange.id”和normalize space(.)='+df_temp.age+'))))。click()仍然不起作用,因为你
df_temp['age']
有值
16-18
,而不是
16-18
,在
-
之前和之后都有空格,不单击它并给出超时例外使用
位于
的元素的可见性,而不是
要单击的元素。检查答案并更新给我们。