Selenium WebDriver Basic Commands: Click, Enter Text & Select Dropdown

Form commands are the most basic commands, which we use most of the time. These commands are used to enter text, click on links, click on buttons, selecting dropdowns, select radio buttons, select checkboxes, select date from date picker etc.

Form Commands - SendKeys, Clear, GetText & Click:

  • Sendkeys - You can enter text in any text box using this command like., username, password, description etc.
/* sendKeys()
 *  This command is used for entering value in a Text box
driver.findElement(By.xpath("/some/xpath")).sendKeys("Some Value");

// OR
// Implementation with Webelement
WebElement FIRSTNAME =  driver.findElement(By.xpath("/some/xpath"));
FIRSTNAME.sendKeys("Any Text value");

  • Clear - This command is used to clear the text from the text box. This command is generally used to clear pre-filled text values from text boxes.
// Clear Command - to clear the already filled data in text box

  • GetText - This command is used to get value/text of a web element. It's a very important command.
/* getText() Command
 *  This command is used to get value/text from a webelement
String textValue = driver.findElement(By.xpath("/some/xpath")).getText();

  • Click - This command is used most of the time. It is used to click on any type of web element like., 
    • Link
    • Button
    • Radio button
    • Check box
    • Date Picker / Calendar
/* Click Command
 *  click() 

Select Dropdown Commands
Select commands can be used to select and deselect dropdown options. We can select a dropdown by 3 different commands:

1. selectByIndex() - We can select any dropdown value by just giving it's index/position at which it is present. Index starts from 0. So, if we have to select 2nd dropdown option then we have to give it's idex as 1, for e.g., selectByIndex(1)

2. selectByVisibleText() - This command is used select dropdown by the it's text value. Like if I have to select option Europe then the command would be selectByVisibleText("Europe").

3. selectByValue() - This command is used to select dropdown option by providing the it's option tag's value attribute. You have to look in the html for value attribute of it's option tag.
Something like,  [option value = "As"] Asia [/option]

So the command would be - selectByValue("As")

Code for all Select and De-select Commands

/* Select Commands
 *  selectByIndex()
 *  selectByVisibleText()
 *  selectByValue()
WebElement DROP_DOWN = driver.findElement(By.xpath("/select"));

// Create object of Select Class
Select select1 = new Select(DROP_DOWN);

// Option 1

// Option 2

// Option 3  ---->> html code 

// De-Select Commands

// Get all selected options, returns a list

// Returns WebElement of first selected option 

// Check if checkbox is multi-select or not, return True or False

// Example - Get all options from a dropdown and print them
WebElement select = driver.findElement(By.tagName("select"));

// Get all options in a list with tagname 'option'
List allOptions = select.findElements(By.tagName("option"));

// Print all options in for each loop
for (WebElement option : allOptions) {
 System.out.println(String.format("Value is: %s", option.getAttribute("value")));;

Wait Commands  << Previous     ||    Next >>  Webdriver Script (with basic commands)

Follow Us

Quora Space | Facebook Page | Telegram Channel
Feel free to ask queries or share your thoughts in comments or email us.


Popular posts from this blog

10 Best Demo Websites for Automation Testing Practice

Automate 'User Registration' of E-commerce Website using Selenium

Automate E-Commerce Website using Selenium Webdriver

Automate Browser Actions on using Selenium