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.
driver.get("https://www.selenium.dev/selenium/web/inputs.html");// Click on the element WebElementcheckInput=driver.findElement(By.name("checkbox_input"));checkInput.click();
packagedev.selenium.elements;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.By;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.chrome.ChromeDriver;importjava.time.Duration;import staticorg.junit.jupiter.api.Assertions.assertEquals;publicclassInteractionTest{@TestpublicvoidinteractWithElements(){WebDriverdriver=newChromeDriver();driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));// Navigate to Urldriver.get("https://www.selenium.dev/selenium/web/inputs.html");// Click on the element WebElementcheckInput=driver.findElement(By.name("checkbox_input"));checkInput.click();BooleanisChecked=checkInput.isSelected();assertEquals(isChecked,false);//SendKeys// Clear field to empty it from any previous dataWebElementemailInput=driver.findElement(By.name("email_input"));emailInput.clear();//Enter TextStringemail="admin@localhost.dev";emailInput.sendKeys(email);//VerifyStringdata=emailInput.getAttribute("value");assertEquals(data,email);//Clear Element// Clear field to empty it from any previous dataemailInput.clear();data=emailInput.getAttribute("value");assertEquals(data,"");driver.quit();}}
# Navigate to URLdriver.get("https://www.selenium.dev/selenium/web/inputs.html")# Click on the checkboxcheck_input=driver.find_element(By.NAME,"checkbox_input")check_input.click()
fromseleniumimportwebdriverfromselenium.webdriver.common.byimportByimportpytestdeftest_interactions():# Initialize WebDriverdriver=webdriver.Chrome()driver.implicitly_wait(0.5)# Navigate to URLdriver.get("https://www.selenium.dev/selenium/web/inputs.html")# Click on the checkboxcheck_input=driver.find_element(By.NAME,"checkbox_input")check_input.click()is_checked=check_input.is_selected()assertis_checked==False# Handle the email input fieldemail_input=driver.find_element(By.NAME,"email_input")email_input.clear()# Clear fieldemail="admin@localhost.dev"email_input.send_keys(email)# Enter text# Verify inputdata=email_input.get_attribute("value")assertdata==email# Clear the email input field againemail_input.clear()data=email_input.get_attribute("value")assertdata==""# Quit the driverdriver.quit()
// Navigate to Urldriver.Navigate().GoToUrl("https://www.selenium.dev/selenium/web/inputs.html");// Click on the element IWebElementcheckInput=driver.FindElement(By.Name("checkbox_input"));checkInput.Click();
usingSystem;usingMicrosoft.VisualStudio.TestTools.UnitTesting;usingOpenQA.Selenium;usingOpenQA.Selenium.Chrome;namespaceSeleniumDocs.Elements{ [TestClass]publicclassInteractionTest{ [TestMethod]publicvoidTestInteractionCommands(){IWebDriverdriver=newChromeDriver();driver.Manage().Timeouts().ImplicitWait=TimeSpan.FromMilliseconds(500);// Navigate to Urldriver.Navigate().GoToUrl("https://www.selenium.dev/selenium/web/inputs.html");// Click on the element IWebElementcheckInput=driver.FindElement(By.Name("checkbox_input"));checkInput.Click();//VerifyBooleanisChecked=checkInput.Selected;Assert.AreEqual(isChecked,false);//SendKeys// Clear field to empty it from any previous dataIWebElementemailInput=driver.FindElement(By.Name("email_input"));emailInput.Clear();//Enter TextStringemail="admin@localhost.dev";emailInput.SendKeys(email);//VerifyStringdata=emailInput.GetAttribute("value");Assert.AreEqual(data,email);//Clear Element// Clear field to empty it from any previous dataemailInput.Clear();data=emailInput.GetAttribute("value");//VerifyAssert.AreEqual(data,"");//Quit the browserdriver.Quit();}}}
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Element Interaction'dolet(:driver){start_session}before{driver.get'https://www.selenium.dev/selenium/web/inputs.html'}it'clicks an element'dodriver.find_element(name:'color_input').clickendit'clears and send keys to an element'dodriver.find_element(name:'email_input').cleardriver.find_element(name:'email_input').send_keys'admin@localhost.dev'endend
// 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.
// Clear field to empty it from any previous dataWebElementemailInput=driver.findElement(By.name("email_input"));emailInput.clear();//Enter TextStringemail="admin@localhost.dev";emailInput.sendKeys(email);
packagedev.selenium.elements;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.By;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.chrome.ChromeDriver;importjava.time.Duration;import staticorg.junit.jupiter.api.Assertions.assertEquals;publicclassInteractionTest{@TestpublicvoidinteractWithElements(){WebDriverdriver=newChromeDriver();driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));// Navigate to Urldriver.get("https://www.selenium.dev/selenium/web/inputs.html");// Click on the element WebElementcheckInput=driver.findElement(By.name("checkbox_input"));checkInput.click();BooleanisChecked=checkInput.isSelected();assertEquals(isChecked,false);//SendKeys// Clear field to empty it from any previous dataWebElementemailInput=driver.findElement(By.name("email_input"));emailInput.clear();//Enter TextStringemail="admin@localhost.dev";emailInput.sendKeys(email);//VerifyStringdata=emailInput.getAttribute("value");assertEquals(data,email);//Clear Element// Clear field to empty it from any previous dataemailInput.clear();data=emailInput.getAttribute("value");assertEquals(data,"");driver.quit();}}
# Handle the email input fieldemail_input=driver.find_element(By.NAME,"email_input")email_input.clear()# Clear fieldemail="admin@localhost.dev"email_input.send_keys(email)# Enter text
fromseleniumimportwebdriverfromselenium.webdriver.common.byimportByimportpytestdeftest_interactions():# Initialize WebDriverdriver=webdriver.Chrome()driver.implicitly_wait(0.5)# Navigate to URLdriver.get("https://www.selenium.dev/selenium/web/inputs.html")# Click on the checkboxcheck_input=driver.find_element(By.NAME,"checkbox_input")check_input.click()is_checked=check_input.is_selected()assertis_checked==False# Handle the email input fieldemail_input=driver.find_element(By.NAME,"email_input")email_input.clear()# Clear fieldemail="admin@localhost.dev"email_input.send_keys(email)# Enter text# Verify inputdata=email_input.get_attribute("value")assertdata==email# Clear the email input field againemail_input.clear()data=email_input.get_attribute("value")assertdata==""# Quit the driverdriver.quit()
//SendKeys// Clear field to empty it from any previous dataIWebElementemailInput=driver.FindElement(By.Name("email_input"));emailInput.Clear();//Enter TextStringemail="admin@localhost.dev";emailInput.SendKeys(email);
usingSystem;usingMicrosoft.VisualStudio.TestTools.UnitTesting;usingOpenQA.Selenium;usingOpenQA.Selenium.Chrome;namespaceSeleniumDocs.Elements{ [TestClass]publicclassInteractionTest{ [TestMethod]publicvoidTestInteractionCommands(){IWebDriverdriver=newChromeDriver();driver.Manage().Timeouts().ImplicitWait=TimeSpan.FromMilliseconds(500);// Navigate to Urldriver.Navigate().GoToUrl("https://www.selenium.dev/selenium/web/inputs.html");// Click on the element IWebElementcheckInput=driver.FindElement(By.Name("checkbox_input"));checkInput.Click();//VerifyBooleanisChecked=checkInput.Selected;Assert.AreEqual(isChecked,false);//SendKeys// Clear field to empty it from any previous dataIWebElementemailInput=driver.FindElement(By.Name("email_input"));emailInput.Clear();//Enter TextStringemail="admin@localhost.dev";emailInput.SendKeys(email);//VerifyStringdata=emailInput.GetAttribute("value");Assert.AreEqual(data,email);//Clear Element// Clear field to empty it from any previous dataemailInput.Clear();data=emailInput.GetAttribute("value");//VerifyAssert.AreEqual(data,"");//Quit the browserdriver.Quit();}}}
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Element Interaction'dolet(:driver){start_session}before{driver.get'https://www.selenium.dev/selenium/web/inputs.html'}it'clicks an element'dodriver.find_element(name:'color_input').clickendit'clears and send keys to an element'dodriver.find_element(name:'email_input').cleardriver.find_element(name:'email_input').send_keys'admin@localhost.dev'endend
const{suite}=require('selenium-webdriver/testing');const{By,Browser}=require('selenium-webdriver');constassert=require("node:assert");suite(function(env){describe('Element Interactions',function(){letdriver;before(asyncfunction(){driver=awaitenv.builder().build();});after(async()=>awaitdriver.quit());it('should Clear input and send keys into input field',asyncfunction(){try{awaitdriver.get('https://www.selenium.dev/selenium/web/inputs.html');letinputField=awaitdriver.findElement(By.name('no_type'));awaitinputField.clear();awaitinputField.sendKeys('Selenium');assert.strictEqual(awaitinputField.getText(),"Selenium");}catch(e){console.log(e)}});});},{browsers:[Browser.CHROME,Browser.FIREFOX]});
// 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.
//Clear Element// Clear field to empty it from any previous dataemailInput.clear();
packagedev.selenium.elements;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.By;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.chrome.ChromeDriver;importjava.time.Duration;import staticorg.junit.jupiter.api.Assertions.assertEquals;publicclassInteractionTest{@TestpublicvoidinteractWithElements(){WebDriverdriver=newChromeDriver();driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));// Navigate to Urldriver.get("https://www.selenium.dev/selenium/web/inputs.html");// Click on the element WebElementcheckInput=driver.findElement(By.name("checkbox_input"));checkInput.click();BooleanisChecked=checkInput.isSelected();assertEquals(isChecked,false);//SendKeys// Clear field to empty it from any previous dataWebElementemailInput=driver.findElement(By.name("email_input"));emailInput.clear();//Enter TextStringemail="admin@localhost.dev";emailInput.sendKeys(email);//VerifyStringdata=emailInput.getAttribute("value");assertEquals(data,email);//Clear Element// Clear field to empty it from any previous dataemailInput.clear();data=emailInput.getAttribute("value");assertEquals(data,"");driver.quit();}}
fromseleniumimportwebdriverfromselenium.webdriver.common.byimportByimportpytestdeftest_interactions():# Initialize WebDriverdriver=webdriver.Chrome()driver.implicitly_wait(0.5)# Navigate to URLdriver.get("https://www.selenium.dev/selenium/web/inputs.html")# Click on the checkboxcheck_input=driver.find_element(By.NAME,"checkbox_input")check_input.click()is_checked=check_input.is_selected()assertis_checked==False# Handle the email input fieldemail_input=driver.find_element(By.NAME,"email_input")email_input.clear()# Clear fieldemail="admin@localhost.dev"email_input.send_keys(email)# Enter text# Verify inputdata=email_input.get_attribute("value")assertdata==email# Clear the email input field againemail_input.clear()data=email_input.get_attribute("value")assertdata==""# Quit the driverdriver.quit()
//Clear Element// Clear field to empty it from any previous dataemailInput.Clear();data=emailInput.GetAttribute("value");
usingSystem;usingMicrosoft.VisualStudio.TestTools.UnitTesting;usingOpenQA.Selenium;usingOpenQA.Selenium.Chrome;namespaceSeleniumDocs.Elements{ [TestClass]publicclassInteractionTest{ [TestMethod]publicvoidTestInteractionCommands(){IWebDriverdriver=newChromeDriver();driver.Manage().Timeouts().ImplicitWait=TimeSpan.FromMilliseconds(500);// Navigate to Urldriver.Navigate().GoToUrl("https://www.selenium.dev/selenium/web/inputs.html");// Click on the element IWebElementcheckInput=driver.FindElement(By.Name("checkbox_input"));checkInput.Click();//VerifyBooleanisChecked=checkInput.Selected;Assert.AreEqual(isChecked,false);//SendKeys// Clear field to empty it from any previous dataIWebElementemailInput=driver.FindElement(By.Name("email_input"));emailInput.Clear();//Enter TextStringemail="admin@localhost.dev";emailInput.SendKeys(email);//VerifyStringdata=emailInput.GetAttribute("value");Assert.AreEqual(data,email);//Clear Element// Clear field to empty it from any previous dataemailInput.Clear();data=emailInput.GetAttribute("value");//VerifyAssert.AreEqual(data,"");//Quit the browserdriver.Quit();}}}
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Element Interaction'dolet(:driver){start_session}before{driver.get'https://www.selenium.dev/selenium/web/inputs.html'}it'clicks an element'dodriver.find_element(name:'color_input').clickendit'clears and send keys to an element'dodriver.find_element(name:'email_input').cleardriver.find_element(name:'email_input').send_keys'admin@localhost.dev'endend
const{suite}=require('selenium-webdriver/testing');const{By,Browser}=require('selenium-webdriver');constassert=require("node:assert");suite(function(env){describe('Element Interactions',function(){letdriver;before(asyncfunction(){driver=awaitenv.builder().build();});after(async()=>awaitdriver.quit());it('should Clear input and send keys into input field',asyncfunction(){try{awaitdriver.get('https://www.selenium.dev/selenium/web/inputs.html');letinputField=awaitdriver.findElement(By.name('no_type'));awaitinputField.clear();awaitinputField.sendKeys('Selenium');assert.strictEqual(awaitinputField.getText(),"Selenium");}catch(e){console.log(e)}});});},{browsers:[Browser.CHROME,Browser.FIREFOX]});
// 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.