Interacting with web elements
There are only 5 basic commands that can be executed on an element:
- click (applies to any element)
- send keys (only applies to text fields and content editable elements)
- clear (only applies to text fields and content editable elements)
- submit (only applies to form elements)
- select (see Select List Elements)
Additional validations
These methods are designed to closely emulate a user’s experience, so, unlike the Actions API, it attempts to perform two things before attempting the specified action.
- If it determines the element is outside the viewport, it scrolls the element into view, specifically it will align the bottom of the element with the bottom of the viewport.
- It ensures the element is interactable before taking the action. This could mean that the scrolling was unsuccessful, or that the element is not otherwise displayed. Determining if an element is displayed on a page was too difficult to define directly in the webdriver specification, so Selenium sends an execute command with a JavaScript atom that checks for things that would keep the element from being displayed. If it determines an element is not in the viewport, not displayed, not keyboard-interactable, or not pointer-interactable, it returns an element not interactable error.
Click
The element click command is executed on the center of the element. If the center of the element is obscured for some reason, Selenium will return an element click intercepted error.
// Navigate to Url
driver.get("https://www.selenium.dev/selenium/web/inputs.html");
// Click on the element
driver.findElement(By.name("color_input")).click();
# Navigate to url
driver.get("https://www.selenium.dev/selenium/web/inputs.html")
# Click on the element
driver.find_element(By.NAME, "color_input").click()
// Navigate to Url
driver.Navigate().GoToUrl("https://www.selenium.dev/selenium/web/inputs.html");
// Click the element
driver.FindElement(By.Name("color_input")).Click();
# Navigate to URL
driver.get 'https://www.selenium.dev/selenium/web/inputs.html'
# Click the element
driver.find_element(name: 'color_input').click
// Navigate to Url
await driver.get('https://www.selenium.dev/selenium/web/inputs.html');
// Click the element
await driver.findElement(By.name('color_input')).click();
// Navigate to Url
driver.get("https://www.selenium.dev/selenium/web/inputs.html")
// Click the element
driver.findElement(By.name("color_input")).click();
Send keys
The element send keys command
types the provided keys into an editable element.
Typically, this means an element is an input element of a form with a text
type or an element
with a content-editable
attribute. If it is not editable,
an invalid element state error is returned.
Here is the list of possible keystrokes that WebDriver Supports.
// Navigate to Url
driver.get("https://www.selenium.dev/selenium/web/inputs.html");
// Clear field to empty it from any previous data
driver.findElement(By.name("email_input")).clear();
//Enter Text
driver.findElement(By.name("email_input")).sendKeys("admin@localhost.dev");
# Navigate to url
driver.get("https://www.selenium.dev/selenium/web/inputs.html")
# Clear field to empty it from any previous data
driver.find_element(By.NAME, "email_input").clear()
# Enter Text
driver.find_element(By.NAME, "email_input").send_keys("admin@localhost.dev" )
// Navigate to Url
driver.Navigate().GoToUrl("https://www.selenium.dev/selenium/web/inputs.html");
// Clear field to empty it from any previous data
driver.FindElement(By.Name("email_input")).Clear();
//Enter Text
driver.FindElement(By.Name("email_input")).SendKeys("admin@localhost.dev");
}
# Navigate to URL
driver.get 'https://www.selenium.dev/selenium/web/inputs.html'
# Clear field to empty it from any previous data
driver.find_element(name: 'email_input').clear
# Enter Text
driver.find_element(name: 'email_input').send_keys 'admin@localhost.dev'
// Navigate to Url
await driver.get('https://www.selenium.dev/selenium/web/inputs.html');
//Clear field to empty it from any previous data
await driver.findElement(By.name('email_input')).clear();
// Enter text
await driver.findElement(By.name('email_input')).sendKeys('admin@localhost.dev');
// Navigate to Url
driver.get("https://www.selenium.dev/selenium/web/inputs.html")
//Clear field to empty it from any previous data
driver.findElement(By.name("email_input")).clear()
// Enter text
driver.findElement(By.name("email_input")).sendKeys("admin@localhost.dev")
Clear
The element clear command resets the content of an element.
This requires an element to be editable,
and resettable. Typically,
this means an element is an input element of a form with a text
type or an element
with acontent-editable
attribute. If these conditions are not met,
an invalid element state error is returned.
// Navigate to Url
driver.get("https://www.selenium.dev/selenium/web/inputs.html");
// Clear field to empty it from any previous data
driver.findElement(By.name("email_input")).clear();
# Navigate to url
driver.get("https://www.selenium.dev/selenium/web/inputs.html")
# Clear field to empty it from any previous data
driver.find_element(By.NAME, "email_input").clear()
// Navigate to Url
driver.Navigate().GoToUrl("https://www.selenium.dev/selenium/web/inputs.html");
// Clear field to empty it from any previous data
driver.FindElement(By.Name("email_input")).Clear();
}
# Navigate to URL
driver.get 'https://www.selenium.dev/selenium/web/inputs.html'
# Clear field to empty it from any previous data
driver.find_element(name: 'email_input').clear
// Navigate to Url
await driver.get('https://www.selenium.dev/selenium/web/inputs.html');
//Clear field to empty it from any previous data
await driver.findElement(By.name('email_input')).clear();
// Navigate to Url
driver.get("https://www.selenium.dev/selenium/web/inputs.html")
//Clear field to empty it from any previous data
driver.findElement(By.name("email_input")).clear()
Submit
In Selenium 4 this is no longer implemented with a separate endpoint and functions by executing a script. As such, it is recommended not to use this method and to click the applicable form submission button instead.