2

How do I get the selected value in the dropdown

HTML Code:

<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>

enter image description here

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
npm
  • 15
  • 3

2 Answers2

2

To get the selected value in the dropdown you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CssSelector:

    SelectElement status = new SelectElement(driver.FindElement(By.CssSelector("select[name='appealStatusId']")));
    IWebElement selected = status.SelectedOption;
    Console.Write(selected.Text);
    
  • Using XPath:

    SelectElement status = new SelectElement(driver.FindElement(By.XPath("//select[@name='appealStatusId']")));
    IWebElement selected = status.SelectedOption;
    Console.Write(selected.Text);
    
  • Using Name:

    SelectElement status = new SelectElement(driver.FindElement(By.Name("appealStatusId")));
    IWebElement selected = status.SelectedOption;
    Console.Write(selected.Text);
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

You can select the element based on it's Value, Text or Id in dropdown options.

By using text:

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();
          }
     }

Or by using 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");
Akshay
  • 169
  • 1
  • 4