How to Enter the text into Input Field(textbox or text area) ?

>The following are the different ways you can type text into the text box or text area fields using Selenium:  

>Using sendKeys()        

Test Steps :

      • Launch the Firefox browser
      • Navigate to AppUrl
      • Enter Keyword into text box      
 
Selenium Code :
package seleniumProject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;  
 
public class EnterText_Into_InputField_Using_SendKeys {  
      public static void main(String[] args) {  
 
 // Launch the Firefox browser  
 
WebDriver driver = new FirefoxDriver();  
 
// Navigate to AppUrl    
 
driver.get("https://www.kosmiktechnologies.com/jobs/");  
 
// Enter Keyword into text box    
 
driver.findElement(By.id("keyword")).sendKeys("Kosmik Technologies");  
     }
}        

>    Using JavascriptExecutor

      Test Steps :
 
      • Launch the Firefox browser
      • Navigate to AppUrl ü Identify Enter Keyword text box
      • Enter Keyword into text box using javascript      

Selenium Code :

package seleniumProject;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;  
public class EnterText_Into_InputField_Using_Javascript {  
public static void main(String[] args) {    
 
// Launch the Firefox browser
 
WebDriver driver = new FirefoxDriver();
 
// Navigate to AppUrl  
 
driver.get("https://www.kosmiktechnologies.com/jobs/");
 
// Identify Enter Keyword text box
 
WebElement EnterKeyword = driver.findElement(By.id("keyword"));
 
// Enter Keyword into text box using javascript  
 
((JavascriptExecutor) driver).executeScript("arguments[0].value = 'Kosmik Technologies';",EnterKeyword);
 
   }
 
}            

>Using Action Class

    Test Steps :
 
      • Launch the Firefox browser
      • Navigate to AppUrl ü Identify Enter Keyword text box
      • create action class object for that driver
      • Enter the Keyword(i.e Enter Kosmik Technologies Keyword)
      • Closing current driver window

Selenium Code :

package ActionClassPack;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.interactions.Actions;    

public class EnterText_Into_InputField_Using_ActionClass {

public static void main(String[] args) {

// Launch the Firefox browser

WebDriver driver = new FirefoxDriver();    

// Navigate to AppUrl    

driver.get("https://www.kosmiktechnologies.com/jobs/");

// Identify Enter Keyword text box

WebElement EnterKeyword = driver.findElement(By.id("keyword"));    

// create action class object for that driver

Actions act = new Actions(driver);

// Enter the Keyword(i.e Enter Kosmik Technologies Keyword)

act.sendKeys(EnterKeyword,"kosmik Technologies").perform();    

// Closing current driver window

driver.close();

     }

}

 

What is the difference between findElement() and findElements()?

    • findElement() : By using this we can identify single webelement On a webpage.  

Ex :

Button, checkbox,radio button etc.

Syntax :

driver.findElement(By.Locator("Locator-Value")); 
 
TestSteps :
      • open the firefox browser
      • navigate the application url
      • Enter the username
      • Close the current Firefox Browser

Selenium Code :

package seleniumProject;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;      

public class findElementMethod {

public static void main(String[] args) throws InterruptedException {

// open the firefox browser

WebDriver driver = new FirefoxDriver();  

// navigate the application url  

driver.get("http://www.kosmiktechnologies.com/seleniumLiveProject/eKart/admin/");  

// Enter the username  

driver.findElement(By.name("username")).sendKeys("Kadmin");  

// Close the current Firefox Browser  

driver.close();

        }
 
}      
 
    • findElements() : By using this we can identify list of webelements at a time On a webpage.  

Ex :

    • Suppose if you want to identify dropdown values at a time then we use findElements method.
    • Suppose if you want to identify list of frames at a time in a webpage then we use findElements method.
    • Suppose if you want to identify list of links at a time in a webpage then we use findElements method.  

Syntax :

  >List elementName = driver.findElements(By.Locator("LocatorValue"));

TestSteps :

    • open the firefox browser
    • navigate the application url
    • Identify all the links and store into total_Links variable
    • Print the total Links on a webpage
    • Close the current Firefox Browser window    

Selenium Code :

package seleniumProject;

import java.util.List;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;      

public class findElementsMethod {  

public static void main(String[] args) throws InterruptedException {

// open the firefox browser  

WebDriver driver = new FirefoxDriver();  

// navigate the application url      

driver.get("http://www.kosmiktechnologies.com/seleniumLiveProject/eKart/admin/");

//Identify all the links and store into total_Links variable

List total_Links = driver.findElements(By.tagName("a"));  

//Print the total Links on a webpage

System.out.println(total_Links.size());  

// Close the current Firefox Browser  

driver.close();

     } 

 

}

Note:

WebElement is an in-built interface in selenium which is used to store anything captured from the web application, it works like a datatype and it can be used to store information about links, images, buttons, radio button, etc.