Webdriver - java - how to unlock keys after performing an action

I use webdriver Action to execute some key combinations:

new Actions(getWebDriver()).sendKeys(Keys.CONTROL, ..).perform(); 

My problem is that Keys.CONTROL remains activated after the action. And after some new clicks, I have new open tabs in my browser and strange behavior due to the key being still activated. How to release a key? Thanks.

+4
source share
3 answers

Create a series of actions, and then complete them. A useful demonstration of how to release a control key is to remove the add-in panel by simulating pressing Control + / , and then sending a keyUp message to release the held Control key:

 WebDriver driver = getDriver(); String handle = driver.getWindowHandle(); // Remove the "add-on" bar. driver.switchTo().defaultContent(); Actions actions = new Actions( driver ); actions .sendKeys( Keys.CONTROL, Keys.DIVIDE ) .keyUp( Keys.CONTROL ) .build() .perform(); // Return back to whatever window was first selected. driver.switchTo().window( handle ); 
+1
source

Try using Keys.chord() . In the documentation:

Simulate the simultaneous pressing of many keys in the "chord". Accepts a sequence of Keys.XXXX or a string; adds each of the values ​​to a string and adds a key to complete the chord (Keys.NULL) and returns the result of the string. Note. When the low-level webdriver key handlers see Keys.NULL, the active modifier keys (CTRL / ALT / SHIFT / etc.) using the keyup event.

So, Keys.NULL , which is automatically added using a chord, should release control.

Decision

 new Actions(getWebDriver()).sendKeys(Keys.chord(Keys.CONTROL, ...)).perform(); 
0
source

for me, only such a workaround helped:

 new Actions(selenium).sendKeys(Keys.ESCAPE).perform(); 
0
source

Source: https://habr.com/ru/post/1445097/


All Articles