Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从一个长长的下拉列表中单击使用selenium的元素?_Python_Selenium_Selenium Webdriver - Fatal编程技术网

Python 如何从一个长长的下拉列表中单击使用selenium的元素?

Python 如何从一个长长的下拉列表中单击使用selenium的元素?,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我试图单击一个元素,比如说下拉列表中的国家列表,但我只能使用xpath单击前几个国家,当我尝试单击最后一个国家时,单击似乎不起作用。下面是代码(它适用于前几个国家,但我想单击下拉列表中的最后一个国家) 如果有人帮助我,我将不胜感激 from selenium import webdriver from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by impo

我试图单击一个元素,比如说下拉列表中的国家列表,但我只能使用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() 

如果您最不想点击的国家/地区,您可以使用selenium单击下拉列表中的滚动条,然后选择元素?您可以共享HTML吗?@MosheSlavin您可以打开url@SohanDas你需要在问题中加入相关的HTML。虽然添加一个URL很好,但是页面内容可以在没有通知的情况下更改,如果没有它,这个问题对于未来的读者来说将没有任何价值。特别是因为你从问题中删除了实际的URL…@JeffC我将添加