1.

How do you clear the contents of a text box?

Answer»

Selenium WebDriver has predefined method CLEAR() which is used to clear the text entered or displayed in the text fields. Some times default text is displayed in the text box or sometimes due to cookies stored pre-populated text is displayed in a text box and we need to clear that text before entering NEW text otherwise we might get the WRONG result. To AVOID test failure it is always a best practice to clear text before entering new text.

driver.findElement(By.id(“id Value”)).clear();

public void clearTheTextBoxField () { _driver.findElement ( By.cssSelector ( “#textbox1” )).clear (); }

Let us see the code for clear text. We will use google.com for our reference. In this, we are sending some text first and then clear it.

package testPackage; import java.awt.AWTException; import java.io.IOException; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import multiScreenShot.MultiScreenShot; public class textClear  {     public static void main(STRING[] args) throws IOException, AWTException      {         WebDriver driver =new FirefoxDriver();         driver.get("https://google.com");         //maximize the window         driver.manage().window().maximize();         WebElement searchBox = driver.findElement(By.name("q"); //Send text to the web element searchBox.sendKeys("Selenium clear text testing"); //clear text searchBox.Clear();    } }


Discussion

No Comment Found