Selenium WebDriver - Form Commands: Enter and Get Text, Click & Select Dropdowns

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
driver.findElement(By.xpath("/some/xpath")).clear();
  • 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() 
 */
driver.findElement(By.xpath("/some/xpath")).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
select1.selectByIndex(2);

// Option 2
select1.selectByVisibleText("Edam");

// Option 3  ---->> html code 
select1.selectByValue("Edam");


// De-Select Commands
select1.deselectAll();
select1.deselectByIndex(index);
select1.deselectByValue(arg0);
select1.deselectByVisibleText(arg0);


// Get all selected options, returns a list
select1.getAllSelectedOptions();


// Returns WebElement of first selected option 
select1.getFirstSelectedOption();


// Check if checkbox is multi-select or not, return True or False
select1.isMultiple();


// 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")));
  option.click();
}




Join Telegram channel at https://t.me/techlistic
Feel free to ask queries or share your thoughts in comments or email me.

Comments

Popular Posts

Top 10 Demo Websites to Practice Selenium Webdriver Online

Future of Selenium Automation Testing as a Career Option

How to learn Selenium Webdriver on your own online

Selenium WebDriver - Take Screenshot and Partial Screenshot Commands

Selenium Webdriver - Overview & Features