How to select the drop down using Selenium/NUnit C#

Viewed 84

Here is my control:

<input name="ctl00$ContentMain$dockAlert$C$ucAlerts$ddlViewBy" type="text" class="rcbInput radPreventDecorate" id="ctl00_ContentMain_dockAlert_C_ucAlerts_ddlViewBy_Input" value="Active" readonly="readonly" autocomplete="off">

Here is how I am TRYING to access it: (tried all 3)

SelectElement ddlViewBy = new SelectElement(driver.FindElement(By.Name("ddlViewBy")));
SelectElement ddlViewBy = new SelectElement(driver.FindElement(By.Id("ddlViewBy")));
SelectElement ddlViewBy = new SelectElement(driver.FindElement(By.Id("ctl00_ContentMain_dockAlert_C_ucAlerts_ddlViewBy_Input")));

I must be missing something simple.

1 Answers

The desired WebElement is a dynamic element, so to identify the element you have to induce WebDriverWait for the ElementIsVisible() and you can use either of the following Locator Strategies:

  • CssSelector:

    SelectElement ddlViewBy = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.CssSelector("input.rcbInput.radPreventDecorate#ctl00_ContentMain_dockAlert_C_ucAlerts_ddlViewBy_Input")));
    
  • XPath:

    SelectElement ddlViewBy = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//input[@id='ctl00_ContentMain_dockAlert_C_ucAlerts_ddlViewBy_Input' and @class='rcbInput radPreventDecorate']")));
    
Related