Home > Software engineering >  Action class keyDown and keyUp Selenium Java
Action class keyDown and keyUp Selenium Java

Time:04-18

Code trials:

searchbx.sendKeys("computer");
Thread.sleep(6000);
Actions action=new Actions(driver);
action.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).build().perform();
action.keyDown(Keys.CONTROL).sendKeys("c").keyUp(Keys.CONTROL).build().perform();
Thread.sleep(6000);
searchbx.clear();
Thread.sleep(6000);
searchbx.click();
action.keyDown(Keys.CONTROL).sendKeys("v").keyUp(Keys.CONTROL).build().perform();

I am trying to run this but am getting the following error:

INFO: Detected dialect: W3C
Exception in thread "main" java.lang.IllegalArgumentException: keyDown argument must be an instanceof Keys: null
    at org.openqa.selenium.interactions.Actions.asKeys(Actions.java:219)
    at org.openqa.selenium.interactions.Actions.keyDown(Actions.java:115)
    at protrainingtech.automationtrainingcourse.Key.main(Key.java:22)

I am trying to run the above code in selenium but getting the above error.

CodePudding user response:

If you just want to simulate "CTRL a" and then CTRL "c" you can use the below code: (using searchbx webelement as an example from your question)

Code:

searchbx.sendKeys("computer");
Thread.sleep(6000);
Actions action = new Actions(driver);
//action.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).build().perform();
//action.keyDown(Keys.CONTROL).sendKeys("c").keyUp(Keys.CONTROL).build().perform();
searchbx.SendKeys(Keys.LEFT_CONTROL   "a");
searchbx.SendKeys(Keys.LEFT_CONTROL   "c"); 
Thread.sleep(6000);
searchbx.clear();
Thread.sleep(6000);
searchbx.click();
searchbx.sendKeys(Keys.LEFT_CONTROL   "v");

You can interchangeably use CONTROL also in this case.

CodePudding user response:

You need to add the following import:

import org.openqa.selenium.Keys;

However, as per the Key down documentation you can use the following code block:

Actions actionProvider = new Actions(driver);
Action keydown = actionProvider.keyDown(Keys.CONTROL).sendKeys("a").build();
keydown.perform();
  • Related