Showing posts with label How to clear the text from the text box without using clear() method.. Show all posts
Showing posts with label How to clear the text from the text box without using clear() method.. Show all posts

Wednesday, June 4, 2014

How to clear the text from the text box without using clear() method.

There could be many ways to do this.

1) Use Control+a then backspace-

Example-

import java.util.Scanner;

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

public class Clear {
    public static void main(String[] args) throws InterruptedException {
        Scanner in = new Scanner(System.in);
        System.out.println("enter the string which you want to send and then clean");
        String input = in.next();
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.google.co.in/?gfe_rd=cr&ei=zQmQU9D3OoyK8QfpxoDQBA&gws_rd=ssl");
        WebElement box = driver.findElement(By.name("q"));
        box.sendKeys(input);
        Thread.sleep(5000); //simply wait to see that input string has been sent to box
       
        //this for block is used to clear the input from box
        //this we can do by using clear method but clear sometime won't work so use this way
        //box.clear();
        box.sendKeys(Keys.chord(Keys.CONTROL,"a"));
        box.sendKeys(Keys.BACK_SPACE);
    }
}   

2) Press the backspace key but here the number of times backspace key pressed should be equal to the number of character in the string because backspace will clear one character in one press.

import java.util.Scanner;

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

public class Clear {
    public static void main(String[] args) throws InterruptedException {
        Scanner in = new Scanner(System.in);
        System.out.println("enter the string which you want to send and then clean");
        String input = in.next();
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.google.co.in/?gfe_rd=cr&ei=zQmQU9D3OoyK8QfpxoDQBA&gws_rd=ssl");
        WebElement box = driver.findElement(By.name("q"));
        box.sendKeys(input);
        Thread.sleep(5000); //simply wait to see that input string has been sent to box
      
        //this for block is used to clear the input from box
        //this we can do by using clear method but clear sometime won't work so use this way
        //box.clear();
        for(int i=0; i<input.length();i++){
            box.sendKeys(Keys.BACK_SPACE);
        }
    }
}