简体   繁体   English

JQuery UI从下拉列表中选择项目Selenium c#

[英]JQuery UI select Item from dropdown Selenium c#

I am new to Selenium and I need to test a page that uses JqueryUI extensivily. 我是Selenium的新手,我需要测试一个使用JqueryUI的页面。

to take an example everyone can work on Let's have a look at http://view.jqueryui.com/master/demos/selectmenu/default.html page 举一个例子,每个人都可以参与进来让我们来看看http://view.jqueryui.com/master/demos/selectmenu/default.html页面

now from the "Select a speed" dropdown i want to select "Fast" 现在从“选择速度”下拉列表我想选择“快速”

My understading is that at runtime jqueryUI makes the following code not visible and creates "UL-LI" therefore the "SelectElement" function cannot be used. 我的理解是,在运行时,jqueryUI使以下代码不可见并创建“UL-LI”,因此不能使用“SelectElement”函数。

    View Source=

      <label for="speed">Select a speed</label>
        <select name="speed" id="speed">
          <option>Slower</option>
          <option>Slow</option>
          <option selected="selected">Medium</option>
          <option>Fast</option>
          <option>Faster</option>
        </select>

Runtime "Inspect element" you get:

<div class="ui-selectmenu-menu ui-front ui-selectmenu-open" style="top: 94.5938px; left: 22px;">
    <ul aria-hidden="false" aria-labelledby="speed-button" id="speed-menu" role="listbox" tabindex="0" 
        class="ui-menu ui-corner-bottom ui-widget ui-widget-content" aria-activedescendant="ui-id-26" aria-disabled="false" style="width: 256px;">
        <li class="ui-menu-item">
          <div id="ui-id-26" tabindex="-1" role="option" class="ui-menu-item-wrapper ui-state-active">Slower</div>
        </li>
        <li class="ui-menu-item">
          <div id="ui-id-27" tabindex="-1" role="option" class="ui-menu-item-wrapper">Slow</div>
        </li>
        <li class="ui-menu-item"><div id="ui-id-28" tabindex="-1" role="option" class="ui-menu-item-wrapper">Medium</div></li>
        <li class="ui-menu-item"><div id="ui-id-29" tabindex="-1" role="option" class="ui-menu-item-wrapper">Fast</div>
        </li><li class="ui-menu-item"><div id="ui-id-30" tabindex="-1" role="option" class="ui-menu-item-wrapper">Faster</div>
        </li>
    </ul>
</div>

What I would like to do is 我想做的是

  1. find me the speed-menu element 找到我的速度菜单元素
  2. click the speed-menu element 单击speed-menu元素
  3. find all the li items and select the "Faster" one 找到所有的李项目并选择“更快”的项目

How do I do this? 我该怎么做呢? I did find an link that with a solution to my same problem but I cannot make it work in c# https://groups.google.com/forum/#!msg/selenium-users/uWmH_XuxcPM/2GVQnHd2aLIJ 我确实找到了一个解决我同样问题的链接,但我无法在c# https://groups.google.com/forum/#!msg/selenium-users/uWmH_XuxcPM/2GVQnHd2aLIJ

Can you help? 你能帮我吗? Thanks 谢谢

As you have mentioned todos. 正如你所提到的待办事项。 Following code will help you to accomplish the same- 以下代码将帮助您完成相同的事情 -

First you need to click on speed-dropdown span only then it loads the ul and li tags - 首先,您需要点击速度下拉span然后才会加载ulli标签 -

driver.FindElement(By.Id("speed-button")).Click();

Then get all li tags of speed-menu 然后获取speed-menu所有li标签

IList<IWebElement> menuElements = driver.FindElements(By.XPath("//ul[@id='speed-menu']/li"));

And iterate all element, find your match and select that one - 迭代所有元素,找到你的匹配并选择那个 -

 foreach (IWebElement element in menuElements )
 {
    string value= element.FindElement(By.TagName("div")).Text;
    if(value.Equals("Faster"))
    {   
            element.Click();
    }
 }

Note :- Please check syntax as per C# 注意: - 请按照C#检查语法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Selenium C#在Materialise CSS上选择下拉项 - Select Dropdown Item on Materialize CSS using Selenium C# 如何使用硒从C#中的角度下拉列表中选择一个值 - How to select a value from an angular dropdown in C# with selenium 如何使用 Selenium C# 从 Devextreme 下拉列表中 select 的值 - How to select a value from Devextreme dropdown with Selenium C# 如何使用Selenium WebDriver 3.8 for C#从下拉列表中进行选择 - How to select from a dropdown with Selenium WebDriver 3.8 for C# C# Selenium Web 驱动程序:无法从下拉值 Z99938282F04071859941E18F16EFCF4 - C# Selenium Web driver: Not able to select value from dropdown Select 来自标签类型 - 使用 selenium C# webdriver 输入下拉列表 - Select from tag type - input dropdown with selenium C# webdriver 使用硒(C#)从文本下拉菜单中选择一个项目 - Using Selenium (C#) Select an item from a drop down by text 如何选择<span>使用Selenium WebDriver和C#</span>在<span>标签</span>下定义的下拉菜单项 - How to select dropdown menu item which is defined under <span> tag using Selenium WebDriver and C# Selenium:Firefox驱动程序,在C#中使用SelectElement从下拉列表中选择一项无法正常工作 - Selenium : Firefox Driver, Selecting an item from a dropdown using SelectElement in c# not working correctly 使用 selenium c# 获取下拉项目数 - get dropdown item count using selenium c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM