How to select and deselect web element in selenium webdriver.

In this article we are going to see the what are the functions needed to select and dis select any web element found by web driver. I am using c# code(vs2010) here.
From my previous post, we know how to find a web element. To select an web element, we need to declare select element object and then assign the found element as selected. So the code will be
private SelectElement myElement ;
element = driver.FindElement(By.XPath("String"));//finding element

We can select an element by 3 ways.

1. To Select the element option by the index(as determined by the "index" attribute of the element)
myElement.SelectByIndex(12); //Here 12 is an integer

2. To Select all options by the text displayed
myElement.SelectByText("String");

3. To Select an option by the value.
myElement.SelectByValue("string");

Like as selected ,deselect can be done in 3 ways also.
myElement.DeselectByIndex(12);
myElement.DeselectByText("string");
myElement.DeselectByValue("String");

In addition, we can deselect all
myElement.DeselectAll();

...Thanks...:)

No comments:

Post a Comment