Selenium action chains - click multiple items while holding modifier key
I have a scenario where I have to press multiple WebElement
while holding a key CTRL.
The Selenium event generator looks like it was built for this purpose, so I built the following sequence of actions:
@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 it didn't work for me. What he did is select each item separately, but not both.
I've also tried other options with no luck:
- Didn't use
.keyUp
at all -
Define the items manually one by one and then call
.click
for each one, theoretically holding the buttonCTRLWebElement 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();
- Use separate collectors for each item
Am I missing a trick here?
PS I am using Firefox which has to support the class Actions
as it says on the official Selenium website.
EDIT1 The items I am trying to click are Vaadin generated grid cells.
source to share
As you said, you are using Vaadin grid cell items from comment
I have automated a simple flow to select the table contents. Selenium click doesn't work on this. As a workaround, I am changing the class names to select cells. Assigning v-selected
className does 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);
}
}
source to share