我想更改 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)
检查工作代码以及下面的内联解释:
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)