0
votes

Following is the code to press the CONTROL key and select multiple tiles on an HTML file. It is not performing what it is supposed to.

Can someone please help me with it?

public class ActionBuildPerform {

    public static void main(String... args) {

        WebDriver driver = new FirefoxDriver();

        driver.get("file://C:/selectable.html");

        WebElement one = driver.findElement(By.name("one"));
        WebElement three = driver.findElement(By.name("three"));
        WebElement five = driver.findElement(By.name("five"));

        // Add all the actions into the Actions builder.
        Actions builder = new Actions(driver);

        builder.keyDown(Keys.CONTROL)
                .click(one)
                .click(three)
                .click(five)
                .keyUp(Keys.CONTROL);
        // Generate the composite action.

        Action compositeAction = builder.build();
        // Perform the composite action.
        compositeAction.perform();
    }
}
2
Is this not working ? builder.keyDown(Keys.CONTROL) .click(one) .click(three) .click(five) .keyUp(Keys.CONTROL).perform();Amanpreet Kaur
Yes, its not working. The following code does not click select multiple objects on my web page: builder.keyDown(Keys.CONTROL) .click(one) .click(three) .click(five) .keyUp(Keys.CONTROL);SeleniumLearner
Why dont you try clicking the elements using WebElement.click() function. Something like this. Actions builder = new Actions(driver); builder.keyDown(Keys.CONTROL).perform(); one.click(); three.click(); five.click(); builder.keyUp(Keys.CONTROL).perform();Amanpreet Kaur
If I click on one web element as WebElement.click(), then it is working fine. I am facing problem with the line where I am trying to Press CONTROL key plus click on multiple Web elements.SeleniumLearner
Try with the above code. It will select multiple elements.Amanpreet Kaur

2 Answers

0
votes

Use Java Robot class

 try {
              Robot robot = new Robot();

     //ctrl TAB  

            robot.keyPress(KeyEvent.VK_CONTROL);

          .click(one)
          .click(three)
          .click(five)

          robot.keyRelease(KeyEvent.VK_CONTROL);

          } catch (AWTException e) {
                  e.printStackTrace();
          }
}
0
votes

Thank all for your help and replies. I was able to resolve this issue by using Selenium 2.53.0 and Firefox 46.0. It seems I was not using compatible version of browser with my Selenium version.