How to Select an Option from Drop Down Using Selenium Webdriver C#

How to select an option from drop down using Selenium WebDriver C#?

You must create a select element object from the drop down list.

 using OpenQA.Selenium.Support.UI;

// select the drop down list
var education = driver.FindElement(By.Name("education"));
//create select element object
var selectElement = new SelectElement(education);

//select by value
selectElement.SelectByValue("Jr.High");
// select by text
selectElement.SelectByText("HighSchool");

More info here

Select a value from drop down using Selenium WebDriver C#

1) Using a SelectElement as already commented - How to select an option from drop down using Selenium WebDriver C#? The SelectElement belongs to the OpenQA.Selenium.Support.UI namespace.

2) You could also do something like this with css selectors:

WebElement dateOfBirth =  webdriver.FindElement(By.Id("join_birth_day"))
.FindElement(By.CssSelector("option[value='3']")).Select();

Select each option in a drop down using Selenium WebDriver C#

Depending what version of Selenium WebDriver you are using you can use the SelectElement class, which will be included in OpenQA.Selenium.Support.UI.

For example:

SelectElement selector = new SelectElement(element);
selector.SelectByIndex(1);

Where the element is your drop down box.

How to select a drop down item using Selenium in C# from a Listbox with Dynamic (changing) Xpath value on every page load

This might not be a perfect answer, or the best way to do this, but I was able to solve this issue using JavaScript. Used an Array to store the dynamic ids at run time, and later used them to create string to find the required element on DOM. Below is the my code.

int numberOfEntriesOnPage = System.Convert.ToInt32(((IJavaScriptExecutor)driver).ExecuteScript("return document.getElementsByName('Entries.index').length"));
string[] array = new string[numberOfEntriesOnPage];
for (int a = 0; a < numberOfEntriesOnPage; a++)
{
String script = "return document.getElementsByName('Entries.index')[" + a + "].value";
array[a] = ((IJavaScriptExecutor)driver).ExecuteScript(script).ToString();
Console.WriteLine("Array value:" + array[a]);
string rowTypeID = "Entries_" + array[a] + "__TypeID";

select_select_by_index(By.Id("Entries_" + array[a] + "__TypeID"), 1);
IWebElement selectOrg = find_element(By.Name("Entries[" + array[a] + "].OrganisationID_input"));
selectOrg.Clear();
selectOrg.SendKeys("3801 LTD");

IWebElement selectInOffice = driver.FindElement(By.Id("Entries_" + array[a] + "__InOffice"));
selectInOffice.Clear();
selectInOffice.SendKeys("10");

IWebElement selectOffsite = driver.FindElement(By.Id("Entries_" + array[a] + "__Offsite"));
selectOffsite.Clear();
selectOffsite.SendKeys("5");

IWebElement comments = driver.FindElement(By.Id("Entries_" + array[a] + "__Comment"));
comments.Clear();
comments.SendKeys(array[a] + "Manish test");

IWebElement save = find_element(By.XPath("//button[@value='SaveDraft']"));

save.Click();

}

public static void select_select_by_index(By by, int index)
{
var select = new SelectElement(find_element(by));
select.SelectByIndex(index);
}

Selenium-webdriver help needed with selecting dropdown without select option

Please try the following code. I have it in java.

    //click on dropdown
driver.findElement(By.xpath("//button[@data-id='Product_Contractor_Person_Contact_Country']")).click();

//select option
List<WebElement> lstOptions= driver.findElements(By.xpath("//ul[contains(@class,'selectpicker')]/li/a/span"));

selectOption(lstOptions, "Zweden");

public boolean selectOption(List<WebElement> lstOptions,String option){

boolean isOptionAvailable=false;

for(WebElement eleOptions:lstOptions){
if(eleOptions.getText().trim().equals(option.trim())){
isOptionAvailable=true;
eleOptions.click();
break;
}
}

return isOptionAvailable;

}


Related Topics



Leave a reply



Submit