简体   繁体   English

如何清空选择元素或选择空选项? C#Web驱动程序

[英]How to empty the select element or to select empty option? c# webdriver

I need select element to be empty, how can I do that? 我需要选择元素为空,我该怎么办? I tried with selectbytext and empty value, but it did not worked. 我尝试使用selectbytext和空值,但是没有用。

selectElement2.SelectByText(""); //(failed)

<option value=""></option>
<option value="1" selected="selected">Testing1</option>
<option value="2">Testing2</option>
...

You can try 你可以试试

 new Select(driver.findElement(By.xpath("//selection box"))).selectByIndex(0);

 //or

 new Select(driver.findElement(By.xpath("//selection box"))).selectByValue("");

 //or

 driver.findElement(By.xpath("//selection box")).sendKeys("");

此语法可能会有所帮助

$('#DdlSiteName').empty();

You can use Deselect method from Select class 您可以从Select类使用Deselect方法

selectElement2.DeselectByValue("1");
// or
selectElement2.DeselectByIndex(1);
// or
selectElement2.DeselectByText("Testing1");

In the case you presented, it should just be 在您提出的情况下,应该只是

selectElement2.SelectByValue("");

Take a look at the docs for .Net 看看.Net的文档

SelectByIndex() , SelectByText() , and SelectByValue() are available. SelectByIndex()SelectByText()SelectByValue()可用。 Index is by the index of the OPTION tags, starting with 0, so index = 1 is "Testing1". 索引是从OPTION标记的索引开始的,从0开始,因此索引= 1为“ Testing1”。 NOTE: Don't confuse index with value, in this case they are the same. 注意:不要将索引与值混淆,在这种情况下它们是相同的。 Text is the visible text in the dropdown, eg "Testing1" in your sample is currently selected. 文本是下拉菜单中的可见文本,例如,当前已选择样本中的“ Testing1”。 Value is the value inside the OPTION tag, eg "1" for "Testing1". 值是OPTION标记内的value ,例如,“ Testing1”的value “ 1”。

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

相关问题 选择元素问题 - webdriver - c# - Select element issue - webdriver - c# C#XmlDocument选择节点返回空 - C# XmlDocument select nodes returns empty 使用Selenium WebDriver C#在下拉列表中选择每个选项 - Select each option in a 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 WebDriver C# 从下拉列表中选择一个选项? - How to select an option from drop down using Selenium WebDriver C#? 如何使用Selenium Webdriver C#从弹出式下拉列表中选择一个选项 - how to select an option from the popup dropdown list using selenium webdriver c# 用C#选择选项 - Select option with C# 使用Webdriver C#选择具有动态ID的下拉元素 - Select a dropdown element with dynamic ID using Webdriver, C# 如何点击 <select>元素以显示在硒webdriver C#中显示的选项? - How to click <select> element to make the options displayed in selenium webdriver 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?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM