Handling Listbox in Selenium



Handling Listbox in Selenium

 

[Test]
 public void InteractWithListbox()
 {
     IWebDriver dr = new ChromeDriver();
     dr.Navigate().GoToUrl("https://output.jsbin.com/osebed/2");
     IWebElement fruitsLB = dr.FindElement(By.XPath("//select[@id='fruits']"));
     SelectElement objSelect = new SelectElement(fruitsLB);
     Console.WriteLine("Multi select allowed:" + objSelect.IsMultiple);
     objSelect.SelectByValue("apple");
     objSelect.SelectByText("Grape");
     Console.WriteLine("Selected options count before:" + objSelect.AllSelectedOptions.Count);
     objSelect.DeselectByText("Apple");
     
     //objSelect.DeselectAll(); //Deselect all selected options
     //objSelect.DeselectByText("Grape"); //Deselect Grape option
     Console.WriteLine("Selected options count after:" + objSelect.AllSelectedOptions.Count);
 
 }
Tags: , ,
Leave a comment

Your email address will not be published. Required fields are marked *

Subscribe now

Receive weekly newsletter with educational materials, new courses, most popular posts, popular books and much more!