Handling Textboxes in Selenium
Handling textboxes in selenium will discuss about how to handle textboxes on a webpage. Basically a textbox is an input tag and will accept character sequence. While automating any application if you want to automate any textbox in selenium you have two types of options to enter data into the textbox. One is using sendKeys() method of selenium webdriver and another is using JavaScriptExecutor.
While entering into the data we need to remember some of the points:
- You need to use sendKeys() method to enter data into textbox after identifying the webelement on the webpage.
- If already a textbox contains data in it and you try to enter the data into same textbox then the data will be appended to the existing data.
- If you want to enter the data which is already having data in it. Then you need to clear the textbox before entering data into it.
- To clear the textbox we can use clear() method of selenium.
- Sometimes selenium methods will not work and not able to identify the elements and not able to enter data into it. Then we can take the help of JavaScriptExecutor to enter data into the textbox.
Now will see the examples for the above statements:
1. Use sendKeys() method to enter the data into textbox.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class HandleTextbox { @Test public void textBox() { System.setProperty("webdriver.gecko.driver", "/KRISHNA VOLUME/drivers/geckodriver"); WebDriver driver = new FirefoxDriver(); driver.get("http://demo.automationtesting.in/Register.html"); driver.findElement(By.xpath("//*[@id='basicBootstrapForm']/div[1]/div[1]/input")).sendKeys("Krishna"); } }
2. Append data in a Textbox.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class HandleTextbox { @Test public void textBox() { System.setProperty("webdriver.gecko.driver", "/KRISHNA VOLUME/drivers/geckodriver"); WebDriver driver = new FirefoxDriver(); driver.get("http://demo.automationtesting.in/Register.html"); driver.findElement(By.xpath("//*[@id='basicBootstrapForm']/div[1]/div[1]/input")).sendKeys("Krishna"); driver.findElement(By.xpath("//*[@id='basicBootstrapForm']/div[1]/div[1]/input")).sendKeys("Sakinala"); } }
3. Clear the Textbox using clear() method.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class HandleTextbox { @Test public void textBox() { System.setProperty("webdriver.gecko.driver", "/KRISHNA VOLUME/drivers/geckodriver"); WebDriver driver = new FirefoxDriver(); driver.get("http://demo.automationtesting.in/Register.html"); driver.findElement(By.xpath("//*[@id='basicBootstrapForm']/div[1]/div[1]/input")).sendKeys("Krishna"); driver.findElement(By.xpath("//*[@id='basicBootstrapForm']/div[1]/div[1]/input")).clear(); } }
4. JavaScriptExecutor to enter data into textbox.
import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class HandleTextbox { @Test public void textBox() { System.setProperty("webdriver.gecko.driver", "/KRISHNA VOLUME/drivers/geckodriver"); WebDriver driver = new FirefoxDriver(); driver.get("http://demo.automationtesting.in/Register.html"); JavascriptExecutor js = (JavascriptExecutor)driver; js.executeScript("document.querySelector(\"input[placeholder='First Name']\").value='Krishna'"); } }
This way we can handle textboxes while automating the application using selenium webdriver.
Please watch YouTube video for better understanding.