简体   繁体   中英

How to send single key (e.g. 'k') to window in Selenium WebDriver with Java?

I'm writing some automated tests for website that mostly uses keyboard shortcuts for navigation. Some of them are with special keys ( Ctrl + V , Alt + C ) and with those I have no problem.

But when I try to send single letters or num-pad symbols, it's not working. When I try to send it to non-text field or root element, it returns exception:

org.openqa.selenium.WebDriverException: unknown error: cannot focus element

I also tried Actions, but they are designed for special ( Ctrl , Alt , Shift ) key combination.

I'm working with Java Selenium Web-driver for Chrome.

Any suggestions?

Apparently Chrome Driver has some issues with sending keys to non-text fields.

I changed ChromeDriver to FirefoxDriver and it's working now. Cannot figure how make it work for Chrome, though.

这对我有用

import org.openqa.selenium.Keys;

driver.findElement(By.xpath("..enter your xpath..")).sendKeys(Keys.K);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM