2

I am trying to select a row in a table by clicking it then moving to a second, holding shift or control and clicking that row to highlight both rows. I am having a problem with this using selenium webdriver and java. I can't see why this code doesn't work? It will select the first row then highlights the second also but when it clicks the second row the first is deselected? This is the code I am using:

  new Actions(driver)
            .moveToElement(selectConsentRow)
            .click()
            .moveToElement(secondRow)
            .keyDown(Keys.SHIFT)
            .click()
            .perform();
2
  • 1
    Your title says CTRL but your code says SHIFT, typo? Just a wonder. Looking at the code through, it appears to be 'correct'. I do notice you're missing a .build() before the .perform(). Have you tried with that? Commented Jul 3, 2013 at 9:44
  • Yeah I have tried it with both. Yes I have added .build() to see if that worked and it didn't. Any other suggestions cause I can't think of anything. Commented Jul 3, 2013 at 9:54

2 Answers 2

2

If you are not against using jquery, Then you can try this

driver.findelement(By.cssSelector(selectConsentRow_css_locator)).click();
   String script = "e = jQuery.Event('click');e.ctrlKey = true;    $('secondRow_Css_locator').trigger(e);";
   js.executeScript(script);
Sign up to request clarification or add additional context in comments.

Comments

1

I have managed to solve this with the following code:

 Robot robot = new Robot();
    selectConsentRow.click();
    robot.keyPress(KeyEvent.VK_CONTROL);
    secondRow.click();
    robot.keyRelease(KeyEvent.VK_CONTROL);

1 Comment

I think you should avoid doing this. This code will not work if you use a RemoteDriver.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.