[ACCEPTED]-Select a value from drop down using Selenium WebDriver C#-selenium-webdriver
To select an Option from Drop Down use the 3 below code
To select a value based on Text
new SelectElement(driver.FindElement(By.XPath(""))).SelectByText("");
To 2 select a value based on Value
new SelectElement(driver.FindElement(By.XPath(""))).SelectByValue("");
To select a 1 value based on Index
new SelectElement(driver.FindElement(By.XPath(""))).SelectByIndex(0);
1) Using a SelectElement as already commented 3 - How to select an option from drop down using Selenium WebDriver C#? The SelectElement belongs to the OpenQA.Selenium.Support.UI namespace.
2) You 2 could also do something like this with css 1 selectors:
WebElement dateOfBirth = webdriver.FindElement(By.Id("join_birth_day"))
.FindElement(By.CssSelector("option[value='3']")).Select();
Use the Following Class SelectElement defined 6 in OpenQA.Selenium.Support.UI namespace 5 the word Select
is already used in C# that is 4 why its implementation is changed and class 3 is named differently.
// Summary:
// Initializes a new instance of the SelectElement class.
//
// Parameters: element - The element to be wrapped
//
public SelectElement(IWebElement element);
Create an object of 2 this class and there is option of selection 1 based on index, text and value.
// Summary:
// Select the option by the index, as determined by the "index" attribute of
// the element.
//
// Parameters:
// index:
// The value of the index attribute of the option to be selected.
public void SelectByIndex(int index);
// Summary:
// Select all options by the text displayed.
//
// Parameters:
// text:
// The text of the option to be selected. If an exact match is not found, this
// method will perform a substring match.
// Remarks:
// When given "Bar" this method would select an option like:
// <option value="foo">Bar</option>
public void SelectByText(string text);
// Summary:
// Select an option by the value.
//
// Parameters:
// value:
// The value of the option to be selected.
// Remarks:
// When given "foo" this method will select an option like:
// <option value="foo">Bar</option>
public void SelectByValue(string value);
using System;
using System.Collections.Generic;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
namespace SeleniumTests {
class DropDownListSelection {
static void Main(string[] args) {
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://YourDDListpageURL.html");
IWebElement element = driver.FindElement(By.XPath("//Select"));
//You can locate the element by using the ID / Name as well IList
AllDropDownList = element.FindElements(By.XPath("//option"));
int DpListCount = AllDropDownList.Count;
for (int i = 0; i < DpListCount; i++) {
if (AllDropDownList[i].Text == "Coffee") {
AllDropDownList[i].Click();
}
}
Console.WriteLine(DpListCount);
Console.ReadLine();
}
}
}
0
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.