使用classname的Selenium webdriver下拉列表选择 [英] Selenium webdriver dropdown selection using classname

查看:67
本文介绍了使用classname的Selenium webdriver下拉列表选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < div class =k-list-scrollerunselectable =onstyle =height:200px;> 
< ul id =Salutation_listboxclass =k-list k-resetunselectable =ontabindex = - 1aria-hidden =falsearia-live =offdata-role =staticlistrole =listbox>
< li id =c77e68b5-ded1-48a8-932c-74a5b2f45c66class =k-item k-state-selected k-state-focuseddata-offset-index =0unselectable =on role =optiontabindex = - 1> Salutation< / li>
< li class =k-itemdata-offset-index =1unselectable =onrole =optiontabindex = - 1style => Mr< / li>
< li class =k-itemdata-offset-index =2unselectable =onrole =optiontabindex = - 1> Mrs< / li>
< li class =k-itemdata-offset-index =3unselectable =onrole =optiontabindex = - 1> Miss< / li>
< li class =k-itemdata-offset-index =4unselectable =onrole =optiontabindex = - 1> Ms< / li>
< li class =k-itemdata-offset-index =5unselectable =onrole =optiontabindex = - 1> Dr< / li>
< li class =k-itemdata-offset-index =6unselectable =onrole =optiontabindex = - 1> Prof< / li>
< li class =k-itemdata-offset-index =7unselectable =onrole =optiontabindex = - 1> Rev< / li>
< / ul>
< / div>
< / div>

这是一个称呼下拉,我需要从下拉值中选择Mr。我的代码不完整我不知道如何为它编写脚本,但我的代码如下,

  driver。 findElement(By.className( K-输入))点击(); 
Thread.sleep(1000);
driver.findElement(By.className(k-item))。click();
driver.findElement(By.tagName(Mr))。click();


解决方案

因为它是



如你所见,元素是 span ,类 k-widget k-dropdown k-header



你必须先点击它。然后只有你将填充ul列表。



从那里你可以选择任何 k-item 使用简单的xpath。


// ul [@ id ='idOfUl'] / li [@ class ='k-item'和text ()='itemName']


对于你来说它应该是


// ul [@ id ='Salutation_listbox'] / li [@ class ='k-item'和text()='Mr']


示例代码

  public void testKendo(){ 
driver.get(http://demos.telerik.com/kendo-ui/dropdownlist/index);

String capColorDropDown =// span [@ role ='listbox'];

driver.findElement(By.xpath(capColorDropDown))。click();

String itemName =Orange;

String listId =color_listbox;

字符串xpathForItem =// ul [@ id ='+ listId +'] / li [@ class ='k-item'和text()='+ itemName +' ];

driver.findElement(By.xpath(xpathForItem))。click();

driver.quit();
}


<div class="k-list-scroller" unselectable="on" style="height: 200px;">
<ul id="Salutation_listbox" class="k-list k-reset" unselectable="on" tabindex="-1" aria-hidden="false" aria-live="off" data-role="staticlist" role="listbox">
<li id="c77e68b5-ded1-48a8-932c-74a5b2f45c66" class="k-item k-state-selected k-state-focused" data-offset-index="0" unselectable="on" role="option" tabindex="-1">Salutation</li>
<li class="k-item" data-offset-index="1" unselectable="on" role="option" tabindex="-1" style="">Mr</li>
<li class="k-item" data-offset-index="2" unselectable="on" role="option" tabindex="-1">Mrs</li>
<li class="k-item" data-offset-index="3" unselectable="on" role="option" tabindex="-1">Miss</li>
<li class="k-item" data-offset-index="4" unselectable="on" role="option" tabindex="-1">Ms</li>
<li class="k-item" data-offset-index="5" unselectable="on" role="option" tabindex="-1">Dr</li>
<li class="k-item" data-offset-index="6" unselectable="on" role="option" tabindex="-1">Prof</li>
<li class="k-item" data-offset-index="7" unselectable="on" role="option" tabindex="-1">Rev</li>
</ul>
</div>
</div>

This is a salutation drop down, I need to select the Mr from the drop down values. My code is incomplete I don't know how to write a script for it, but my code is as follows,

driver.findElement(By.className("k-input")).click();
Thread.sleep(1000);
driver.findElement(By.className("k-item")).click();
driver.findElement(By.tagName("Mr")).click();

解决方案

As it is a KendoUI DropDown

You have to first click on the dropdown element.Could be span or div

I have taken the Demo Site to demsonstrate how you can select the values

I'm going to select Orange value from Cap Color DropDown

As you can see the element is span with class k-widget k-dropdown k-header.

You have to click on that first. Then only you'll get the ul list populated.

From that you can select any k-item by using the simple xpath.

//ul[@id='idOfUl']/li[@class='k-item' and text()='itemName']

For you it should be

//ul[@id='Salutation_listbox']/li[@class='k-item' and text()='Mr']

Sample Code

public void testKendo() {
        driver.get("http://demos.telerik.com/kendo-ui/dropdownlist/index");

        String capColorDropDown = "//span[@role='listbox']";

        driver.findElement(By.xpath(capColorDropDown)).click();

        String itemName = "Orange";

        String listId = "color_listbox";

        String xpathForItem = "//ul[@id='" + listId + "']/li[@class='k-item' and text()='" + itemName + "']";

        driver.findElement(By.xpath(xpathForItem)).click();

        driver.quit();
    }

这篇关于使用classname的Selenium webdriver下拉列表选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆