Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select multiple elements in Selenium (not <Select> - <Option>)

Tags:

c#

list

selenium

How can I select multiple web elements from an ordered list using Selenium WebDriver in C#?

I want to do it manually, I would press Ctrl and select multiple options. I want to automate this.

Below is the sample HTML:

<ol id="selectable" class="ui-selectable">
        <li class="ui-state-default ui-selectee">1</li>
        <li class="ui-state-default ui-selectee">2</li>
        <li class="ui-state-default ui-selectee">3</li>
        <li class="ui-state-default ui-selectee">4</li>
        <li class="ui-state-default ui-selectee">5</li>
        <li class="ui-state-default ui-selectee">6</li>
        <li class="ui-state-default ui-selectee">7</li>
        <li class="ui-state-default ui-selectee">8</li>
        <li class="ui-state-default ui-selectee">9</li>
        <li class="ui-state-default ui-selectee">10</li>
        <li class="ui-state-default ui-selectee">11</li>
        <li class="ui-state-default ui-selectee">12</li>
</ol> 

Appreciate your time and efforts.

like image 857
Ramesh V K Avatar asked Nov 07 '22 23:11

Ramesh V K


1 Answers

Have you tried using GetElementsByClassName()? Combined with the KeyDown command in the Actions class (See the answer for this question) something like this may work for you...

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Interactions;

var driver = new WebDriver();

var elements = driver.GetElementsByClassName("ui-state-default ui-selectee");

var action = new Actions(driver);

action.KeyDown(Keys.Control);

foreach (var element in elements)
{

    // If element properties match your selection
    element.Click()

}
like image 105
jdmac020 Avatar answered Nov 14 '22 16:11

jdmac020