How to Handle Text Box in Selenium using Java
//To click on text box in seleniumdriver.findElement(By.xpath("//input[@type='text']"));
4)How to Get entered/typed Text from a Textbox in Selenium ?getAttribute()- To get the entered text from a textbox by using the selenium us getAttribute() method and also pass "value" as a parameter to the method.
Selenium Code :
//To Get the entered text in selenium
WebElement Wb= driver.findElement(By.xpath("//input[@type='text']"));
Wb.sendKeys("sandeep.uvw@gmail.com");
String txt= Wb.getAttribute("value");
System.out.println(txt);
contains()
using contains() method we can validate /check that text entered or not in the text box.
String txt= Wb.getAttribute("value");
System.out.println(txt);
//TO validate text entered or not in textbox
if (txt.contains("sandeep.uvw@gmail.com"))
{
System.out.println("Yes text is entered ");
}
else
{
System.out.println("Text is not entered ");
}
//To Check that text box is empty or not
String CLbox =Wb.getAttribute("value");
if(CLbox.isEmpty())
{
System.out.println(" Yes Text box is clear");
}
else
{
System.out.println("No Text box is not clear");
}
//To check /verify the place holder in selenium
String plcaehld=Wb.getAttribute("placeholder");
if(plcaehld.equals("Email address or phone number"))
{
System.out.println("Valid Place holder");
}
else
{
System.out.println("invalid place holder");
}
Complete Selenium Code to Handle a Textbox
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class TextBox {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "C:\\Software\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
//Maximize Browser Window
driver.manage().window().maximize();
//Enter URL in Browser
driver.get("https://www.facebook.com//");
driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[@type='text']")).sendKeys("sandeep.uvw@gmail.com");
//To Get the enterd text in selenium
WebElement Wb= driver.findElement(By.xpath("//input[@type='text']"));
String plcaehld=Wb.getAttribute("placeholder");
if(plcaehld.equals("Email address or phone number"))
{
System.out.println("Valid Place holder");
}
else
{
System.out.println("invalid place holder");
String CLbox =Wb.getAttribute("value");
if(CLbox.isEmpty())
{
System.out.println(" Yes Text box is clear");
}
else
{
System.out.println("No Text box is not clear");
}
String txt= Wb.getAttribute("value");
System.out.println(txt);
if (txt.contains("sandeep.uvw@gmail.com"))
{
System.out.println("Yes text is entered ");
}
else
{
System.out.println("Text is not entered ");
}
//Close current browser window
driver.close();
}
}
import org.openqa.selenium.WebDriver;
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "C:\\Software\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.facebook.com");
//Handling Multiple text Box in Selenium
//This is First Textbox
driver.findElement(By.xpath("//input[ @type=\"text\"]")).sendKeys("TextBox1");
//This is Second Textbox
driver.findElement(By.xpath("//input[@type=\"password\"]")).sendKeys("TextBox2");
}
![]() |
How to automate text box in selenium |
Post a Comment
0 Comments