What you will Learn in this blog:

  1. ‘sendkeys’ method in selenium to type text in a text field
  2. Usage of click() method in selenium
  3. Usage of clear() method in selenium
  4. Usage of submit() method in selenium
  5. Source code


‘sendkeys’ method in selenium to type text in a text field

Let us now see the usage of ‘sendkeys’ method in selenium to type some text in a text field. 

Launch https://www.selenium.dev/downloads/ 

Click ‘API Docs’ link under ‘Java’. The below page comes up

Search for ‘By’. You will see search results

Click the first search result shown above.

The selenium API has class ‘By’ that helps in identifying the elements on a webpage

Open chrome browser window and launch https://rahulshettyacademy.com/angularpractice/ 

Inspect the ‘Name’ field. 

Notice below that this field is represented by attribute ‘name’ having the value “name”

See line#17 that shows the usage of By.name method

So we can copy the value of ‘name’ attribute & use it as shown below

Next, we can further chain the ‘sendkeys method that will help us to simulate the typing of keys

So if we want to simulate the typing of “testuser1”, the ‘sendkeys’ method can do it for us

Save and execute the script. 

Notice below that the desired text gets typed in the text field

Usage of click() method in selenium

Go to https://courses.rahulshettyacademy.com/courses 

You will see lot of courses

Let us inspect the first course “ALL-ACCESS Subscription”

We can create xpath to identify this element (this element is present under a ‘div’ tag whose attribute is ‘title’ and the value of attribute is ‘All-Access Membership’)

We can now use the ‘click()’ method to click this element

Save and execute. 

Notice below that the course link is clicked and the course’s detail page comes up

Usage of clear() method in selenium


The ‘clear()’ method simply clears the text from the textbox. See below. We are entering text “testuser1” in line#28. 

In line#29 we are using the ‘clear()’ method to delete or clear the same text    

Save and run. 

Notice below that the ‘Name’ field becomes empty/clear

Usage of submit() method in selenium


Launch https://rahulshettyacademy.com/angularpractice/ 

Enter value in ‘Name’ field

We can see the ‘Submit’ button above to submit the form.

Let us click ‘Submit’. Notice that we get the ‘Success’ message

Let us automate these manual steps

Notice below that the form has been submitted successfully

So this is how we can handle some of the form web elements using selenium.

Source code

package Intro;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


import io.github.bonigarcia.wdm.WebDriverManager;
public class SeleniumScript {
	public static void main(String[] args) {
		WebDriverManager.firefoxdriver().setup();
		WebDriver driver = new FirefoxDriver();
//sendkeys method
		/*		driver.get("https://rahulshettyacademy.com/angularpractice/");		driver.findElement(By.name("name")).sendKeys("testuser1");
		*/
//click method
		/*		driver.get("https://courses.rahulshettyacademy.com/courses");
		driver.findElement(By.xpath("//div[@title='All-Access Membership']")).click();
		*/
//clear method
		/*
		driver.get("https://rahulshettyacademy.com/angularpractice/");		driver.findElement(By.name("name")).sendKeys("testuser1");
	driver.findElement(By.name("name")).clear();
		*/
//submit method		driver.get("https://rahulshettyacademy.com/angularpractice/");		driver.findElement(By.name("name")).sendKeys("testuser1");
	driver.findElement(By.name("name")).submit();
}
}

Thank you for reading!


Tags


You may also like

Groups attribute in TestNG 

Groups attribute in TestNG 
Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}