Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
915 views
in Technique[技术] by (71.8m points)

How to select kendo dropdown element with unselectable="on" attribute using Selenium and Python

Unable to select the kendo dropdown using below code. The site can be reachable for checking the code.

<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')
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To select the item with text as Chang within the kendo dropdown using Selenium you you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using 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()
    
  • Using 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()
    
  • Note : You have to add the following imports :

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

Browser Snapshot: Chang


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...