0

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();
    }
}
Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • Is this not working ? builder.keyDown(Keys.CONTROL) .click(one) .click(three) .click(five) .keyUp(Keys.CONTROL).perform(); – Amanpreet Kaur Sep 08 '16 at 07:30
  • 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 Sep 09 '16 at 16:34
  • 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 Sep 12 '16 at 09:04
  • 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 Sep 14 '16 at 19:10
  • Try with the above code. It will select multiple elements. – Amanpreet Kaur Sep 15 '16 at 10:21

2 Answers2

0

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

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.