简体   繁体   English

如何使用Selenium Webdriver C#从下拉列表中选择列表项的值

[英]How to select a list item from a drop down by its value using Selenium Webdriver C#

I want to select a drop down value. 我想选择一个下拉值。 The problem is the drop down list is derived, it is written as list items. 问题是下拉列表是派生的,它被写为列表项。 Code snippet as follows: 代码片段如下:

<div class="rcbScroll" style="width: 100%; height: 184px; overflow: auto;">
<ul class="rcbList" style="width: 100%">
    <li class="rcbItem">Option 1</li>
    <li class="rcbItem">Option 2</li>
    <li class="rcbItem">Option 3</li>
</ul>

How do I select 'Option 2' as the selected value in the drop down? 如何在下拉菜单中选择“选项2”作为所选值?

using CSS Selectors in Selenium: 在Selenium中使用CSS选择器:

you can get value of nth child by finding it with CSS Selector. 您可以通过使用CSS选择器找到第n个孩子的价值。

// first child: will return "Option 1"
driver.findElement(By.cssSelector("ul.rcbList > li:nth-child(1)"));

// second child: will return "Option 2"
driver.findElement(By.cssSelector("ul.rcbList > li:nth-child(2)"));

// nth child: will return "Option n"
driver.findElement(By.cssSelector("ul.rcbList > li:nth-child(n)"));

In case of when <li> items are dynamic, then get count and loop over and get all the values. 如果<li>项是动态的,则进行计数并循环获取所有值。

var el_count = driver.FindElements(By.CssSelector("ul.rcbList"));

for(int index=0; index < el_count.count(); index++){
   // 0 (zero) is the first element in <ul> DOM Array
   driver.findElement(By.cssSelector("ul > li:nth-child(index)"));
}

Finding element by Text (value) 按文本查找元素(值)

Easier way would be finding by xPath and Class 更简单的方法是通过xPath和Class查找

var title = driver.FindElement(By.XPath("./div[@class='aCont']/div/a/span[text() = 'TextToFind']"));
// now title contains text, title = "text to be find"

for more -Example code 更多- 示例代码

try (only if ul is treated as dropdown): 试试(仅当ul被视为下拉菜单时):

SelectElement selectElement = new SelectElement(driver.FindElement(By.cssSelector(".rcbList")));
selectElement.SelectByText("Option 2");

Reference: 参考:

https://stackoverflow.com/a/31072586/2575259 https://stackoverflow.com/a/31072586/2575259

暂无
暂无

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

相关问题 如何从下拉列表中选择值(Selenium C#) - How to select value from the drop down list (Selenium C#) 使用硒(C#)从文本下拉菜单中选择一个项目 - Using Selenium (C#) Select an item from a drop down by text 如何使用 Selenium WebDriver C# 从下拉列表中选择一个选项? - How to select an option from drop down using Selenium WebDriver C#? 使用C#的Selenium WebDriver:从下拉菜单中选择项(在IE中不起作用) - Selenium WebDriver with C#: select item from drop-down menu (doesn't work in IE) 如何在列表框中使用Selenium选择一个下拉项目,并在每个页面加载时使用动态(更改)Xpath值 - How to select a drop down item using Selenium in C# from a Listbox with Dynamic (changing) Xpath value on every page load 使用Selenium WebDriver C#在下拉列表中选择每个选项 - Select each option in a drop down using Selenium WebDriver C# 无法从 Selenium C# webdriver 的多选下拉列表(组合框)中选择值 - Not able to select value from multi select drop down (Combo box) in Selenium C# webdriver 如何使用C#在Selenium WebDriver中单击列表中的项目? - How to click an item from a list in Selenium WebDriver using C#? 如果下拉菜单不是选择元素,如何将Webdriver与C#一起使用,如何通过文本选择下拉项? - Using webdriver with c# how can I select a drop down item by text if the drop down menu is not a select element? 如何使用 C# 通过 Selenium WebDriver 获取下拉列表中的所有选项? - How to get all options in a drop-down list by Selenium WebDriver using C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM