Friday, September 8, 2023

multiple drop down values selection using control click

// Assuming you have an HTML select element with multiple options
IWebElement multiSelectDropdown = driver.FindElement(By.Id("multiSelectDropdown"));

// To select multiple options, hold down the Control key while clicking
Actions actions = new Actions(driver);
actions.KeyDown(Keys.Control)
       .Click(multiSelectDropdown.FindElement(By.XPath("//option[@value='value1']")))
       .Click(multiSelectDropdown.FindElement(By.XPath("//option[@value='value2']")))
       .Click(multiSelectDropdown.FindElement(By.XPath("//option[@value='value3']")))
       .KeyUp(Keys.Control)
       .Build()
       .Perform();
=≠=====++++other way ======
IWebElement dropdown = driver.FindElement(By.Id("yourDropdownId"));
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;

// Replace "value1", "value2", etc. with the values of the options you want to select
string[] valuesToSelect = new string[] { "value1", "value2", "value3" };

foreach (string value in valuesToSelect)
{
    string script = $"arguments[0].querySelector(\"option[value='{value}']\").selected = true;";
    js.ExecuteScript(script, dropdown);
}

No comments:

Post a Comment