C#中如何获取下拉框的选中值
How to get the selected value of a dropdown in C#
如何在下拉列表中获取选定的值
HTML代码:
<select name="appealStatusId" class="form-control input-sm">
<option value="1">
Pending
</option>
<option value="2">
Overall Appeal Approved
</option>
<option value="3" selected="selected">
Overall Appeal Not Approved
</option>
您可以 select 根据下拉选项中的值、文本或 ID 来选择元素。
通过使用文本:
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("your_URL");
driver.Manage().Window.Maximize();
IWebElement element_name = driver.FindElement(By.Name("appealStatusId"));
SelectElement statusId = new SelectElement(element_name);
// To print all available options
Console.WriteLine(statusId.Options);
// To iterate over the dropdown options and select the one which matches with the text you want to select
foreach(IWebElement element in statusId.Options)
{
if(element.Text == "Overall Appeal Not Approved")
{
element.Click();
}
}
或使用 SelectByValue:
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("your_URL");
driver.Manage().Window.Maximize();
IWebElement element_name = driver.FindElement(By.Name("appealStatusId"));
SelectElement statusId = new SelectElement(element_name);
statusId.SelectByValue("3");
要在下拉列表中获得所选值,您必须引入 WebDriverWait for the element_to_be_clickable()
and you can use either of the following :
使用 CssSelector:
SelectElement status = new SelectElement(driver.FindElement(By.CssSelector("select[name='appealStatusId']")));
IWebElement selected = status.SelectedOption;
Console.Write(selected.Text);
使用 XPath:
SelectElement status = new SelectElement(driver.FindElement(By.XPath("//select[@name='appealStatusId']")));
IWebElement selected = status.SelectedOption;
Console.Write(selected.Text);
使用名称:
SelectElement status = new SelectElement(driver.FindElement(By.Name("appealStatusId")));
IWebElement selected = status.SelectedOption;
Console.Write(selected.Text);
如何在下拉列表中获取选定的值
HTML代码:
<select name="appealStatusId" class="form-control input-sm">
<option value="1">
Pending
</option>
<option value="2">
Overall Appeal Approved
</option>
<option value="3" selected="selected">
Overall Appeal Not Approved
</option>
您可以 select 根据下拉选项中的值、文本或 ID 来选择元素。
通过使用文本:
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("your_URL");
driver.Manage().Window.Maximize();
IWebElement element_name = driver.FindElement(By.Name("appealStatusId"));
SelectElement statusId = new SelectElement(element_name);
// To print all available options
Console.WriteLine(statusId.Options);
// To iterate over the dropdown options and select the one which matches with the text you want to select
foreach(IWebElement element in statusId.Options)
{
if(element.Text == "Overall Appeal Not Approved")
{
element.Click();
}
}
或使用 SelectByValue:
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("your_URL");
driver.Manage().Window.Maximize();
IWebElement element_name = driver.FindElement(By.Name("appealStatusId"));
SelectElement statusId = new SelectElement(element_name);
statusId.SelectByValue("3");
要在下拉列表中获得所选值,您必须引入 WebDriverWait for the element_to_be_clickable()
and you can use either of the following
使用 CssSelector:
SelectElement status = new SelectElement(driver.FindElement(By.CssSelector("select[name='appealStatusId']"))); IWebElement selected = status.SelectedOption; Console.Write(selected.Text);
使用 XPath:
SelectElement status = new SelectElement(driver.FindElement(By.XPath("//select[@name='appealStatusId']"))); IWebElement selected = status.SelectedOption; Console.Write(selected.Text);
使用名称:
SelectElement status = new SelectElement(driver.FindElement(By.Name("appealStatusId"))); IWebElement selected = status.SelectedOption; Console.Write(selected.Text);