如何根据给定的文本单击跨度

时间:2019-11-14 10:43:19

标签: python selenium

考虑一下: 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

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

2 个答案:

答案 0 :(得分:0)

首先单击下拉列表以打开选项列表,然后使用//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()

答案 1 :(得分:0)

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

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)