简体   繁体   English

如何使用Selenium Webdriver C#从弹出式下拉列表中选择一个选项

[英]how to select an option from the popup dropdown list using selenium webdriver c#

I have a telerik searchbox where I enter 'acc' and it automatically pops up the items starting with 'acc' and from that list I want to select 'Acabose'. 我有一个telerik搜索框,在其中输入“ acc”,它会自动弹出以“ acc”开头的项目,然后从该列表中选择“ Acabose”。

C# code: C#代码:

driver.FindElement(By.Id("ctl00_ctl14_tsfSearch_Input")).Click();
driver.FindElement(By.Id("ctl00_ctl14_tsfSearch_Input")).SendKeys("acc");

and HTML: 和HTML:

<div class="rsbSlide">
    <div class="rsbPopup">
        <ul class="rsbList">
            <li class="rsbListItem">Acabose</li>
            <li class="rsbListItem">Acabose 100mg</li>
        </ul>
    </div>
</div>

You could get all of the visible li items and iterate through them until you find the option with the text you want. 您可以获取所有可见的li项目并对其进行遍历,直到找到带有所需文本的选项。 (Note: I work with Selenium in Java, but the principle is the same in c#) (注意:我在Java中使用Selenium,但是原理在c#中是相同的)

// code to enter the search goes here.
List<WebElement> rsbListItems = driver.findElements(By.className("rsbListItem");
for (int i = 0; i < rsbListItems.size(); i++) {
    if (listItem.getText() == "Acabose 100mg") {
        listItem.click();
    }
}

Alternatively, if you know that Acabose is always (and forever) going to be the second item in the list, you can access it by index. 另外,如果您知道Acabose始终(永远)将成为列表中的第二项,则可以按索引访问它。

Try the below snippet 试试下面的代码片段

List<WebElement> telerikItemList = driver.findElements(By.className("rsbListItem");
 for (WebElement telerikItemOpt: telerikItemList) {
   if (telerikItemOpt.getText().equals("Acabose 100mg") {
     telerikItemOpt.click();
     break;
   }
 }

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

相关问题 如何使用Selenium WebDriver 3.8 for C#从下拉列表中进行选择 - How to select from a dropdown with Selenium WebDriver 3.8 for C# 如何使用 Selenium WebDriver C# 从下拉列表中选择一个选项? - How to select an option from drop down using Selenium WebDriver C#? 如何使用 Selenium WebDriver 和 c# 从 Telerik RadComboBox 中选择一个选项? - How to select an option from the Telerik RadComboBox using Selenium WebDriver and c#? Selenium在C#中使用WebDriver选择下拉选项 - Selenium selecting a dropdown option using webdriver in c# 如何使用Selenium Webdriver C#从下拉列表中选择列表项的值 - How to select a list item from a drop down by its value using Selenium Webdriver C# Select 来自标签类型 - 使用 selenium C# webdriver 输入下拉列表 - Select from tag type - input dropdown with selenium C# webdriver 使用Selenium WebDriver C#在下拉列表中选择每个选项 - Select each option in a drop down using Selenium WebDriver C# 如何使用C#在Selenium WebDriver中单击列表中的项目? - How to click an item from a list in Selenium WebDriver using C#? 如何使用Selenium Webdriver C#在chrome弹出窗口上单击OK - How to Click OK on chrome popup using Selenium Webdriver C# 如何选择<span>使用Selenium WebDriver和C#</span>在<span>标签</span>下定义的下拉菜单项 - How to select dropdown menu item which is defined under <span> tag using Selenium WebDriver and C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM