Enter text in a edit box and click on a button

In the previous post, we have seen how to select the random value in a WebList. In this post, we will see how to enter text in a edit box and click on a button.

Enter text into a Edit box:

In our demo application, we have 2 edit boxes ‘First Name’ and ‘Last Name’. Suppose, we need to enter the data into these edit boxes. We have to use the method ‘sendKeys‘ to enter the data.

Edit boxes

Code for entering data in first name and last name:

public class EnterData {
	private static WebDriver driver;
	public static void main(String[] args) throws InterruptedException {
		
		driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		driver.get("https://testingpool.com/wp-content/uploads/2015/08/DemoApplication.html");
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

		WebElement firstName = driver.findElement(By.xpath("//*[@id='firstname']")); 
		WebElement lastName = driver.findElement(By.xpath("//*[@id='lastname']")); 
		firstName.sendKeys("Shekhar");
		lastName.sendKeys("Sharma");
	}
}

Output:

Enter data with selenium

 


 

Click on a button :

We need to create an object of button by using any locator like id, xpath etc. Then, use the method click() to click on that button.

In our application, we have a ‘Submit’ button. When we click on that button, it gives a message ‘Form is submitted’.

To identify button , we use xpath ‘//*[@id=’bttn’]‘.

Code to click on Submit button:

public class ClickButton {
	private static WebDriver driver;
	public static void main(String[] args) throws InterruptedException {
		
		driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		driver.get("https://testingpool.com/wp-content/uploads/2015/08/DemoApplication.html");
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

		WebElement SubmitBttn = driver.findElement(By.xpath("//*[@id='bttn']")); 
		SubmitBttn.click();
	}
}

Output:

Click button in selenium


Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum

 

Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...