WebElement :: sendKeys
of Java Selenium (4.0.0-alpha-4), it got stuck a little.Keys
has the same contents as SHIFT
/ LEFT_SHIFT
" ShiftRight "
in the string without using Keys
, it will be recognized as a right Shift.If you want to keyDown using Selenium in Java, write the following code.
public void sendKey(WebDriver webDriver, CharSequence... keys) {
WebElement element = webDriver.findElement(By.className("hoge"))
element.sendKeys(keys);
}
WebElement.sendKeys
takesCharSequence ...
as an argument, but often uses the provided enumKeys
.
However, be careful if you want to use the same function keys on the left and right, such as the right Shift key / left Shift key.
The implementation of enumKeys
is as follows.
SHIFT
/ LEFT_SHIFT
are defined respectively, but unfortunately the behavior of the left Shift key will behave regardless of which one is sent.
Keys.java
public enum Keys implements CharSequence {
...
SHIFT ('\uE008'),
LEFT_SHIFT (Keys.SHIFT),
CONTROL ('\uE009'),
LEFT_CONTROL (Keys.CONTROL),
ALT ('\uE00A'),
LEFT_ALT (Keys.ALT),
LEFT ('\uE012'),
ARROW_LEFT (Keys.LEFT),
UP ('\uE013'),
ARROW_UP (Keys.UP),
RIGHT ('\uE014'),
ARROW_RIGHT (Keys.RIGHT),
DOWN ('\uE015'),
ARROW_DOWN (Keys.DOWN),
...
}
Keys
implements CharSequence
, and the constructor and toString ()
implementation are as follows.
Keys.java
public enum Keys implements CharSequence {
...
private final char keyCode;
private final int codePoint;
Keys(Keys key) {
this(key.charAt(0));
}
Keys(char keyCode) {
this.keyCode = keyCode;
this.codePoint = String.valueOf(keyCode).codePoints().findFirst().getAsInt();
}
...
@Override
public String toString {
return String.valueOf(keyCode);
}
}
Also, WebElement.sendKeys
only combines the given keycodes with line breaks and sends them to WebDriver.
Therefore, it will be recognized normally by specifying it with a character string as shown below without using the KeyCode defined in Keys.SHIFT
.
WebElement element = webDriver.findElement(By.className("hoge"))
element.sendKeys("ShiftRight");
Recommended Posts