gpt4 book ai didi

python - 如何从长下拉列表中单击使用 Selenium 的元素?

转载 作者:行者123 更新时间:2023-11-28 17:03:10 25 4
gpt4 key购买 nike

我试图点击一个元素,比方说下拉列表中的国家列表,但我只能使用 xpath 点击前几个国家,当我尝试点击最后一个国家时,似乎没有点击工作。这是代码(它适用于前几个国家,但我想从下拉列表中单击最后一个国家)如果有人帮助我,我将不胜感激!

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


driver = webdriver.Chrome()
driver.get('https://www.example.com/dropdown')

##click accept cookies button
wait(driver, 5).until(EC.visibility_of_element_located(
(By.XPATH, '//div[@class="cookie-button-wrapper"]'))).click()

##time delay
time.sleep(20)

##click on specific country from the dropdown
wait(driver, 5).until(EC.visibility_of_element_located(
(By.XPATH, '//div[@class="tv-dropdown__button tv-dropdown-behavior__button tv-screener-market-select__button js-screener-market-button apply-common-tooltip common-tooltip-fixed"]'))).click()
wait(driver, 5).until(EC.visibility_of_element_located(
(By.XPATH, '//*[@data-market="argentina"]'))).click()

最佳答案

首先尝试滚动直到元素:

from selenium.webdriver.common.action_chains import ActionChains

element = driver.find_element_by_xpath("//*[@data-market='italy']")
actions = ActionChains(driver)
actions.move_to_element(element).perform()

然后尝试使用代码的最后一部分点击它:

wait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, '//*[@data-market="italy"]'))).click() 

关于python - 如何从长下拉列表中单击使用 Selenium 的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52972054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com