How to detect an element from the Drop down menu using Variable name.

We all know that we can select the elements from the Drop-down menu using three common methods. i.e. selectByIndex, selectByValue and selectByVisibleText.
But the problem arises when we have to detect the web-element by Variable name.
Here might be the situation,  When we are passing the Variable value by the Variable name  and want to select the option from the Drop-down menu using Variable name then we cannot use any of the available methods to select the Drop-down option.

So in this situation we can try in below manner to get the required Drop-down option from the Drop-down menu.

 WebElement Drop_down_menu = driver.findElement(By.xpath("//select[@name='RubricID']"));  
List Drop_down_option = Drop_down_menu.findElements(By.xpath(".//option[contains(text(),'" + Variable_name_ + "')]"));
for (WebElement option : Drop_down_option)
{
if (!option.isSelected())
{
option.click();
}
}

where   ‘Drop_down_menu‘ will save the path of Drop down box on your web page.
             ‘Drop_down_option‘ will  be the xpath of the required element from the Drop down box in                  which we will use Variable name.
             ‘Variable_name‘  will be the variable name used in your code.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s