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

时间:2018-10-24 14:52:53

标签: python selenium selenium-webdriver

我试图单击一个元素,比方说从下拉列表中列出的国家/地区,但是我只能使用xpath单击前几个国家,而当我尝试单击最后一个国家/地区时,点击似乎无法working.Here是代码(它适用于前几个国家,但我想从下拉列表中单击最后一个国家) 如果有人帮助我,将不胜感激!

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() 

1 个答案:

答案 0 :(得分:1)

首先尝试滚动到元素:

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()