无法使用以下代码选择剑道下拉菜单。可以访问该站点以检查代码。
<span unselectable="on" class="k-dropdown-wrap k-state-default"><span unselectable="on" class="k-input">Chang</span><span unselectable="on" class="k-select" aria-label="select"><span class="k-icon k-i-arrow-60-down"></span></span></span>
Code:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome('./chromedriver')
driver.get("https://demos.telerik.com/kendo-ui/dropdownlist/remotedatasource")
select = driver.find_element_by_xpath('//*[@id="example"]/div/span/span/span[1]')[0]
select.SelectByValue("Chang");
print('Success')
要使用Selenium在剑道下拉菜单中选择文本为Chang的项目,您必须为element_to_be_clickable()
引入WebDriverWait,并且可以使用以下解决方案之一:
使用CSS_SELECTOR
:
driver.get("https://demos.telerik.com/kendo-ui/dropdownlist/remotedatasource")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.k-widget.k-dropdown[aria-owns='products_listbox']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.k-animation-container>div#products-list ul li[data-offset-index='1']"))).click()
使用XPATH
:
driver.get("https://demos.telerik.com/kendo-ui/dropdownlist/remotedatasource")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='k-widget k-dropdown' and @aria-owns='products_listbox']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='k-animation-container']/div[@id='products-list']//ul//li[text()='Chang']"))).click()
注:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC