Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Action chains - click multiple elements while holding a modifier key

I have a scenario where I have to click on multiple WebElements while holding a CTRL modifier key.

Selenium Actions generator looks like it was deigned exactly for that purpose so I built the following actions sequence:

@FindBy(css = "some_css_selector")
private List<WebElement> elements;

for (WebElement element : elements) {
    Actions builder = new Actions(driver);

    builder.keyDown(Keys.CONTROL)
        .click(element)
        .keyUp(Keys.CONTROL);

    Action selectMultiple = builder.build();

    selectMultiple.perform();
    }

So unfortunately that didn't work for me. What it did is selected each element separately but not both together.

I tried other options as well with no luck:

  1. Didn't use .keyUp at all
  2. Define the elements manually one by one and then call the .click on each one of them while theoretically holding the CTRL button

    WebElement el1 = elements.get(0);
    WebElement el2 = elements.get(1);
    
    Actions builder = new Actions(driver);
    
    
    builder.keyDown(Keys.CONTROL)
        .click(el1)
        .click(el2)
        .keyDown(Keys.CONTROL); //tried with and without
    
    Action selected = builder.build();
    selected.perform();
    
  3. Use separate builders for each element

Am I missing some trick here?

P.S. I am using Firefox which should support the Actions class as it says on the official Selenium website.

EDIT1 The elements I am trying to click are Vaadin generated grid cells.

like image 364
Eugene S Avatar asked Aug 09 '26 02:08

Eugene S


1 Answers

As you've said that you are using Vaadin grid Cell elements from the comment

I've automated a simple flow to select the table contents.The selenium click is not working on this. As a workaround I'm changing the classNames to select the cells.Appending v-selected to className do the trick

  WebDriver driver = new ChromeDriver();
    driver.get("http://demo.vaadin.com/sampler/#ui/grids-and-trees/table");
    List<WebElement> elements = driver.findElements(By.xpath("//tr[starts-with(@class,'v-table-row')]"));
    JavascriptExecutor js = (JavascriptExecutor) driver;
    for (WebElement element : elements) {
        if (element.isDisplayed()) {
            js.executeScript("arguments[0].className=arguments[0].className+' v-selected';", element);
        }
    }
like image 91
Madhan Avatar answered Aug 10 '26 16:08

Madhan