Entering data into HTML form with selenium webdriver

selenium

To entering data into html form we can use this sample piece of code of selenium webdriver.
It open the desired webpage with browser and will enter data into webpage.

package com.webdriver_11;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestHtml {

	public WebDriver driver;
	private String baseUrl;
	
	public static void main(String[] args) {
		TestHtml th = new TestHtml();
		th.launch_browser();
		th.my_test();
	}
	public void launch_browser(){
		try{
		 driver = new FirefoxDriver();
		 baseUrl = "http://demo.tanmaysarkar.com/sample_01.html";
		 driver.get(baseUrl);
		 driver.manage().window().maximize();
		 System.out.println("Opening : " + baseUrl);	 	 
		}
		catch (Exception ex)
		{
			ex.getMessage();
		}
	}
	public void my_test()
	{
		try{
			// -- input text
			driver.findElement(By.id("ts_first_name")).sendKeys("tanmay");
			driver.findElement(By.id("ts_last_name")).sendKeys("sarkar");
			// -- text area
			driver.findElement(By.xpath(".//*[@id='ts_address'] ")).sendKeys("My Address"); 
			// -- select
			WebElement selectgender = driver.findElement(By.xpath(".//*[@id='ts_country']"));
			selectgender.sendKeys("India");
			System.out.println("India Selected as Country"); 
			// -- Radio button
			driver.findElement(By.xpath("html/body/form/table/tbody/tr[5]/td[2]/p/label[1]/input")).click();
			System.out.println("Clicked on Gender");
			// -- Checkbox
			driver.findElement(By.xpath(".//*[@id='ts_checkbox1']")).click();
			System.out.println("Cricket Selected");
			driver.findElement(By.xpath(".//*[@id='ts_checkbox3']")).click();
			System.out.println("Hockey Selected");
			
			Thread.sleep(2000); 
			driver.quit();
			System.out.println("Browser closed");
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
}

Related posts:

Leave a Reply

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