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:
.keyUp at allDefine 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();
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.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With