user3411418
user3411418

Reputation: 331

Selecting dropdown values using Select class?

I have two doubts:

  1. Suppose if in a drop down there are two values with the same name, then how can I select the one value from them using Select class?

  2. Suppose in a drop down there is a value "Emergency". I need to run the script in dev and production. In dev url the drop down value is coming in Uppercase i.e EMERGENCY. But in Prod env the drop down value is coming in lowerase i.e Emergency.I need to make the script in such a way that it will select the dropdown value irrespective of the case of the drop down value. I can do it by checking if the env is dev then do this else do this. but i dont want to do like this way. How can i do it using Select class or any other relevant way to do it?

Upvotes: 0

Views: 442

Answers (2)

pavanraju
pavanraju

Reputation: 673

you have other options

  1. select by value

    selectByValue(value);

  2. Select by index

    selectByIndex(index);

Upvotes: 0

Abhijeet Vaikar
Abhijeet Vaikar

Reputation: 1656

Doubt 1: Suppose if in a drop down there are two values with the same name, then how can I select the one value from them using Select class?

You can go for selectByIndex or selectByValue method. So If you know that out of the two options in the drop-down which are same, and you want to select the 2nd one then select the option by index.

Select select = new Select(driver.findElement(By.id("dropDown_id_here")));
select.selectByIndex(2);

If the text of the 2 options is same and the values are different, then you can use:

select.selectByValue("op2");

Doubt 2: Suppose in a drop down there is a value "Emergency". I need to run the script in dev and production. In dev url the drop down value is coming in Uppercase i.e EMERGENCY. But in Prod env the drop down value is coming in lowerase i.e Emergency.I need to make the script in such a way that it will select the dropdown value irrespective of the case of the drop down value. I can do it by checking if the env is dev then do this else do this. but i dont want to do like this way. How can i do it using Select class or any other relevant way to do it?

Select select = new Select(driver.findElement(By.id("dropDown_id_here")));
List<WebElement> options = select.getOptions();

for(WebElement option : options)
 {
   if(option.getText().equalsIgnoreCase("emergency"))
    {
      option.click();
      break;
    }
 }

Please note that I have written the above code on the fly in this editor. It could be syntactically wrong. Kindly just get the idea out of it.

Upvotes: 1

Related Questions