Skip to content

Instantly share code, notes, and snippets.

@SarahElson
Last active September 15, 2022 08:11
Show Gist options
  • Save SarahElson/0a5f63a6980af2fefd8481369fff6164 to your computer and use it in GitHub Desktop.
Save SarahElson/0a5f63a6980af2fefd8481369fff6164 to your computer and use it in GitHub Desktop.
How To Select Dropdown In Selenium C#?
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using System;
using OpenQA.Selenium.Remote;
namespace SeleniumTutorial
{
public class DropDown
{
private static IWebDriver driver;
public static string gridURL = "@hub.lambdatest.com/wd/hub";
public static string LT_USERNAME = "LT_USERNAME";
public static string LT_ACCESS_KEY = "LT_ACCESS_KEY";
[SetUp]
public void Setup()
{
var desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.SetCapability("browserName", "Chrome");
desiredCapabilities.SetCapability("platform", "Windows 11");
desiredCapabilities.SetCapability("version", "101.0");
desiredCapabilities.SetCapability("screenResolution", "1280x800");
desiredCapabilities.SetCapability("user", LT_USERNAME);
desiredCapabilities.SetCapability("accessKey", LT_ACCESS_KEY);
desiredCapabilities.SetCapability("build", "Selenium C-Sharp");
desiredCapabilities.SetCapability("name", "Selenium Test");
driver = new RemoteWebDriver(new Uri($"https://{LT_USERNAME}:{LT_ACCESS_KEY}{gridURL}"), desiredCapabilities, TimeSpan.FromSeconds(600));
}
[Test]
public void ValidateDropDownSelection()
{
string dayOfTheWeek = "Wednesday";
driver.Navigate().GoToUrl("https://www.lambdatest.com/selenium-playground/select-dropdown-demo");
SelectElement dropDown = new SelectElement(driver.FindElement(By.Id("select-demo")));
dropDown.SelectByValue(dayOfTheWeek);
string actualText = driver.FindElement(By.CssSelector(".selected-value.text-size-14")).Text;
Assert.True(actualText.Contains(dayOfTheWeek), $"The expected day of the week {dayOfTheWeek} was not selected. The actual text was: {actualText}.");
}
[TearDown]
public void TearDown()
{
driver.Quit();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment