Mouse and keyboard actions in detail

Suppose you have an arbitrary web element e:


WebElement e = driver.findElement(By.id("testElement"));
  

e = driver.find_element_by_id("testElement")
  

IWebElement e = driver.FindElement(By.Id("testElement"));
  

# We don't have a Ruby code sample yet -  Help us out and raise a PR  
  

// We don't have a JavaScript code sample yet -  Help us out and raise a PR  
  

val element = driver.findElement(By.id("testElement"))
  

You can simulate mouse clicking on e if it is visible and has a height and width that are greater than 0:


e.click();
  

e.click()
  

e.Click();
  

# We don't have a Ruby code sample yet -  Help us out and raise a PR  
  

// We don't have a JavaScript code sample yet -  Help us out and raise a PR  
  

e.click()
  

Moreover, it is possible to mimic hovering of the cursor over e. In order to do so, you will need the following import statement:


import org.openqa.selenium.interactions.Actions;
  

from selenium.webdriver import ActionChains
  

using OpenQA.Selenium.Interactions;
  

# We don't have a Ruby code sample yet -  Help us out and raise a PR
  

// We don't have a JavaScript code sample yet -  Help us out and raise a PR
  

import org.openqa.selenium.interactions.Actions
  

With this statement in place, you can now move over the element in question:


Actions actions = new Actions(driver);
actions.moveToElement(e);
actions.perform();
  

actions = ActionChains(driver)
actions.move_to_element(e)
actions.perform()
  

 Actions actions = new Actions(driver);
 actions.MoveToElement(e);
 actions.Perform();
  

# We don't have a Ruby code sample yet -  Help us out and raise a PR
  

// We don't have a JavaScript code sample yet -  Help us out and raise a PR
  

val actions = Actions(driver)
actions.moveToElement(e);
actions.perform();
  

If e is an input or textarea element, the following keyboard actions can be carried out:

  • Enter a sequence of characters in e:

e.sendKeys("Test");
  

e.send_keys("Test")
  

e.SendKeys("Test");
  

# We don't have a Ruby code sample yet -  Help us out and raise a PR
  

// We don't have a JavaScript code sample yet -  Help us out and raise a PR
  

e.sendKeys("Test")
  
  • Delete the text that is in e (if there is any):

e.clear();
  

e.clear()
  

e.Clear();
  

# We don't have a Ruby code sample yet -  Help us out and raise a PR
  

// We don't have a JavaScript code sample yet -  Help us out and raise a PR
  

e.clear()