user2177781
user2177781

Reputation: 177

Selenium CTRL and click not working?

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();

Upvotes: 2

Views: 1541

Answers (2)

nibu.b
nibu.b

Reputation: 21

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);

Upvotes: 2

user2177781
user2177781

Reputation: 177

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);

Upvotes: 1

Related Questions