Segfam
Segfam

Reputation: 23

Java selenium - Selecting elements in dropDown

I am trying to select an element in a dropDown. my problem is that i can't open the dropDown. What ever i try it doesn't work.

I have located the dropDown :

WebElement elementToClick = driver.findElement(By.cssSelector("a[class='chosen-single']"));

But when i perform a click - Nothing happens.

Any ideas ?

Upvotes: 0

Views: 40

Answers (1)

Apostolos Emmanouilidis
Apostolos Emmanouilidis

Reputation: 7197

In case your dropdown list corresponds to an HTML <select> tag, you could uniquely locate the element (using id, class, XPath etc) and then create a Selenium Select instance:

WebElement element = driver.findElement(By.id("myDropdownElementId"));
Select dropDownSelect = new Select(element);  

After creating the Selenium Select instance, you can use any of the following ways to pick an option from the dropdown list:

// select by index
dropDownSelect.selectByIndex(2);
// select by value
dropDownSelect.selectByValue("Car");
// select by visible text  
dropDownSelect.selectByVisibleText("CarText");  

Upvotes: 1

Related Questions