与下拉列表交互并选择 - Selenium 和 Python

问题描述 投票:0回答:1

我想更改 komoot.com/discover 上的下拉菜单以使用 selenium 和 python 选择一项特定运动,但我似乎无法与之交互。也没有真正理解这个元素的源代码。

我尝试了下面的代码,但没有任何乐趣,只是没有交互和超时。

 try:
        # Choose road cycling from the drop down
        driver.find_element(By.CLASS_NAME,"css-roz6c5-control").click()
        time.sleep(1)
        option = driver.find_element(By.XPATH, f"//div[@id='sport-select']/div[text()='Road cycling']")
        action = ActionChains(driver)
        action.move_to_element(option).click().perform()
        time.sleep(1)
python selenium-webdriver selenium-chromedriver
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

driver = webdriver.Chrome()
driver.get("https://www.komoot.com/discover")
driver.maximize_window()

wait = WebDriverWait(driver,10)
# Click on "Yes, that's fine" cookie button
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@data-testid='gdpr-banner-accept']"))).click()
# Below line will click on the dropdown down arrow
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class=' css-tlfecz-indicatorContainer']"))).click()
# Below line will click on dropdown item "Running". Change the value in the XPath as per your choice
wait.until(EC.element_to_be_clickable((By.XPATH, "(//*[text()='Running'])[1]"))).click()
time.sleep(10)
© www.soinside.com 2019 - 2024. All rights reserved.