Vinod Kumar
Vinod Kumar

Reputation: 13

Dropdown value in Selenium Webdriver using Java

I am in a situation where I have to expand the drop down and from the list of values after expansion, I have to select one value.

Could you please help in this?

Below is the code I have used so far:

List<WebElement> elements = driver.findElement(By.id("Some Value"));

for (WebElement element: elements)
{
    new Actions(driver).sendKeys(Keys.Arrow_Down).perform();
        if(Element.getText().equals("Cliam Document"))
        {
            element.click();
        }
}

Upvotes: 0

Views: 234

Answers (3)

sandeep shewale
sandeep shewale

Reputation: 223

Scenarios:
1. Store value into string where you want to perform click operation.
2. Store all Drop down values into List.
3. Using For each loop store all values into WebElement.
4. Get Text of each and every one value and store into string.
5. Compare each and every Actual string with Expected String.
6. If Expected string found within for each loop then click on WebElement.

String Expected_String = "Test";
List<WebElement> elements = driver.findElement(By.id("Some Value"));
for (WebElement element : elements) 
{
    String value = element.getText();
    if (value.equalsIgnoreCase( Expected_String)) 
    {
        element.click();
        break;
    }
}

Upvotes: 0

sandeep kumar
sandeep kumar

Reputation: 195

You can select a values from Drop down in 3 ways,

  1. By using selectByVisibleText()
  2. By using selectByValue()
  3. By using selectByIndex()

In your case no need to use Action() class. You can use any method from above.

Check this Code:

Select se = new Select(driver.findElement(By.id("your ID")));
se.selectByIndex(your preferred index value);

Upvotes: 0

mohamed faisal
mohamed faisal

Reputation: 177

You can use below code :

to use the dropdownlist

   Select select1 = new Select(driver.findElement(By.id("your identifier")));
    select1.selectByVisibleText("Cliam Document");

Upvotes: 1

Related Questions