Google News
logo
Selenium - Interview Questions
How to send ALT/SHIFT/CONTROL key in Selenium WebDriver?
When we generally use ALT/SHIFT/CONTROL keys, we hold onto those keys and click other buttons to achieve the special functionality. So it is not enough just to specify keys.ALT or keys.SHIFT or keys.CONTROL functions.
 
For the purpose of holding onto these keys while subsequent keys are pressed, we need to define two more methods: keyDown(modifier_key) and keyUp(modifier_key)
 
Parameters : Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONTROL)
Purpose : Performs a modifier key press and does not release the modifier key. Subsequent interactions may assume it’s kept pressed.
 
Parameters : Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONTROL)
Purpose : Performs a key release.

Hence with a combination of these two methods, we can capture the special function of a particular key.
public static void main(String[] args) 
{
String baseUrl = “https://www.facebook.com”;
WebDriver driver = new FirefoxDriver();
 
driver.get("baseUrl");
WebElement txtUserName = driver.findElement(By.id(“Email”);
 
Actions builder = new Actions(driver);
Action seriesOfActions = builder
 .moveToElement(txtUerName)
 .click()
 .keyDown(txtUserName, Keys.SHIFT)
 .sendKeys(txtUserName, “hello”)
 .keyUp(txtUserName, Keys.SHIFT)
 .doubleClick(txtUserName);
 .contextClick();
 .build();
seriesOfActions.perform();
}
Advertisement