Selenium Actions is the key part of test automation to do manipulation on the browsers such as Selenium Click operation, Selenium Send Keys, or Select a Dropdown Box, etc. In this post, all Selenium Actions will be explained in detail. To do these events first we have to find elements in selenium, then do an action on it. Let’s get started!
Selenium Actions
Selenium Webdriver API has got a bunch of methods you can find all the list below link. https://www.mindmeister.com/280141421/selenium-2-webdriver-commands However, the most common selenium webdriver manipulation methods are:
- .click() [Click a button or link]
- .clear() [Clear text]
- .sendKeys(String) [Write text or perform keyup, keydown, etc.]
- .submit [Submit a form]
I want to continue this post with examples. The best way to understand the Selenium Webdriver Actions is to show them in the code as we know “Talk is cheap, show me the code!” ;)
Selenium Mouse Actions
The most common mouse interactions are:
- .click
- .
doubleClick(Deprecated) - .
release(Deprecated) - .
clickAndHold(Deprecated) - .
contextClick(means Right Click) (Deprecated)
First, we have to find the web element which is a button, link, etc. then we will apply mouse action for our test scenario.
WebElement loginButton = driver.findElement(By.cssSelector(".fa.fa-2x.fa-sign-in")); loginButton.click();
Selenium Keyboard Actions
The most common keyboard interactions are:
- .sendKeys
.keyUp(Deprecated)- .
keyDown(Deprecated)
WebElement username = driver.findElement(By.id("username")); username.sendKeys("Hello SW Test Academy!");
Selenium Radio Button and CheckBox Actions
For both, we are using the .click method to select or unselect the elements. In the below code, we are navigating to http://the-internet.herokuapp.com/checkboxes and click both checkboxes and do the assertions.
driver.navigate().to(baseURL + "/checkboxes"); WebElement firstCheckBox = driver.findElement(By.cssSelector("input:nth-of-type(1)")); WebElement secondCheckBox = driver.findElement(By.cssSelector("input:nth-of-type(2)")); firstCheckBox.click(); Assertions.assertTrue(firstCheckBox.isEnabled()); Assertions.assertTrue(firstCheckBox.isSelected()); secondCheckBox.click(); Assertions.assertTrue(secondCheckBox.isEnabled()); Assertions.assertFalse(secondCheckBox.isSelected());
Selenium Drop Down Actions
Our test site is http://the-internet.herokuapp.com/dropdown and our scenario is described as follows.
- Go to the page
- Select the second option with ByIndex
- Select the first option with ByValue
- Select the second option with ByVisibleText
driver.navigate().to(baseURL + "/dropdown"); Select dropDown = new Select(driver.findElement(By.id("dropdown"))); //Select By Index dropDown.selectByIndex(2); //Select By Value dropDown.selectByValue("1"); //Select By Visible Text dropDown.selectByVisibleText("Option 2");
Selenium Multi-Select Actions
Our test site is http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select_multiple and our scenario is described as follows.
- Go to http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select_multiple
- Select Volvo and Open
- Click Submit Query
- Check Volvo and Opel selected
Note: All elements are located in an iframeResult iframe so we have to switch to this iframe to locate elements. First, we need to find the multiple select and then get all the options into a list. After that, we can do all kinds of manipulations with these options.
//Navigate to w3schools.com driver.navigate().to("http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select_multiple"); driver.manage().window().maximize(); //Switch to iframeResult iframe because all elements located in this iframe driver.switchTo().frame("iframeResult"); //Find multiple select and its options WebElement multiSelect = driver.findElement(By.cssSelector("select[name='cars']")); List<WebElement> multiSelectOptions = multiSelect.findElements(By.tagName("option")); //Find submit button WebElement submitButton = driver.findElement(By.cssSelector("input[type='submit']")); //Select Volvo (0) and Opel (2) multiSelectOptions.get(0).click(); multiSelectOptions.get(2).click(); //Click Submit Button submitButton.click(); //Check the result WebElement resultSecondLine = wait.until( ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[class='w3-container w3-large w3-border']"))); System.out.println("ResultText: " + resultSecondLine.getText()); Assertions.assertTrue(resultSecondLine.getText().contains("volvo"), "volvo is not selected!"); Assertions.assertTrue(resultSecondLine.getText().contains("opel"), "opel is not selected!");
Multi-select with Select Class Also, Select webdriver comprises of deselect methods as follows: Deselect methods only work with Multi Selects!
- deselectByIndex
- deselectByValue
- deselectByVisibleText
- deselectAll
@Test @SneakyThrows public void T05_deSelect() { driver.navigate().to("http://www.w3schools.com/tags/tryit.asp?filename=t driver.switchTo().frame("iframeResult"); //Find submit button WebElement submitButton = driver.findElement(By.cssSelector("input[type= //Select Element WebElement multiSelectLocation = driver.findElement(By.name("cars")); //Declare Select Select selectCars = new Select(multiSelectLocation); //Select Volvo (0) and Opel (2) selectCars.selectByVisibleText("Opel"); selectCars.selectByVisibleText("Audi"); Thread.sleep(500); selectCars.deselectAll(); Thread.sleep(500); selectCars.selectByVisibleText("Volvo"); selectCars.selectByVisibleText("Audi"); Thread.sleep(500); //Click Submit Button submitButton.click(); //Check the result Thread.sleep(500); }
Submitting a File in Selenium
Our test site for this example is http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_type_file
- Go to http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_type_file
- Click the “Browse” button
- Select sample_text.txt
- Click the “Submit Query” button
- Check the result
driver.navigate().to("http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_type_file"); driver.manage().window().maximize(); //Switch to iframeResult iframe because all elements located in this iframe driver.switchTo().frame("iframeResult"); //Find the elements WebElement browseButton = driver.findElement(By.cssSelector("body > form:nth-child(3) > input:nth-child(2)")); WebElement submitButton = driver.findElement(By.cssSelector("body > form:nth-child(3) > input:nth-child(5)")); //Test file decleration File testFile = new File(this.getClass().getResource("/test.properties").toURI()); //Select test file browseButton.sendKeys(testFile.getAbsolutePath()); //Click submit button submitButton.click(); //Check the result WebElement resultText = wait.until( ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.w3-container.w3-large.w3-border"))); System.out.println("resulttext: " + resultText.getText()); Assertions.assertTrue(resultText.getText().contains("test.properties"), "test.properties is not submitted!");
Selenium Action Class and Action Sequences
Webdriver provides us to perform interactions in order. We can do that like the sample below code:
Actions builder = new Actions(driver); Action dragAndDrop = builder.clickAndHold(someElement) .moveToElement(otherElement) .release(otherElement) .build(); dragAndDrop.perform();
You can find all other interactions in the manipulation section of the below link. https://www.mindmeister.com/280141421/selenium-2-webdriver-commands
Move an element to another element (Move to)
We should use functions in order.
- clickAndHold()
- moveToElement()
- release()
- perform()
WebElement element1 = driver.findElement(By.id("element1")); WebElement element2 = driver.findElement(By.id("element2")); Actions actions = new Actions(driver); actions.clickAndHold(element1).moveToElement(element2).release().perform();
Drag one element and drop it to another element (Drag and Drop)
We should use the below functions in order.
- dragAndDrop()
- release()
- perform()
WebElement element1 = driver.findElement(By.id("element1")); WebElement element2 = driver.findElement(By.id("element2")); Actions actions = new Actions(driver); actions.dragAndDrop(element1,element2).release().perform();
Key Sequence
For example, if you want to perform ctrl+enter, you can use actions to do this. First we should use keyDown(), then sendKeys(), and finally keyUp().
Actions actions = new Actions(driver); actions.keyDown(Keys.CONTROL). sendKeys(Keys.ENTER). keyUp(Keys.CONTROL). perform();
Github Project
https://github.com/swtestacademy/selenium-examples/tree/main/src/test/java/actions
Thanks,
Onur Baskirt

Onur Baskirt is a Software Engineering Leader with international experience in world-class companies. Now, he is a Software Engineering Lead at Emirates Airlines in Dubai.
Merhaba onur çok güzel bir site açmışsın.Youtube video nasıl yükleyebilirim selenium ile onunla ilgili bir makale hazırlarmısın.Karşılıgında sana bir adet backlink vereceğim blogumdan.
Merhaba Ali, öncelikle bize heyecan veren güzel sözlerin için çok teşekkür ederim. İleride Selenium ile ilgili tüm konuları tamamladıktan sonra bu tarz örneklere yer verebiliriz.Bahsetmiş olduğun örneği önceliklendirmeye çalışırız. Linkedin, facebook ve twitter dan duyurusunu yaparız.
Hello! I just would like to give a huge thumbs up for the great info you have here on this post. I will be coming back to your blog for more soon.
Thanks Nenita.
that’s good, thanks for sharing,.. I think this is great blog
Thank you so much Salvador.
Hi Admin,
Please let me know that how I can select “Feb” in the month by using xpath as I have done below code and it is not working in month case it works for day and year.
WebElement ele_month = driver.findElement(By.id(“month”));
ele_month.findElement(By.xpath(“//*[@value=’2′]”)).click();
Please use this locator: //select[@name=’birthday_month’]//option[@value=’2′]
And I suggest you my Xpath reference article: https:/xpath-selenium/