Divya Divya - 5 months ago 10
Java Question

How to get INPUT field data in eclipse?

I am tried to using following syntaxes but, I didn't get result.

package demo_package;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Class {
public WebDriver driver= new FirefoxDriver();

@BeforeTest
public void beforetest() {
driver.get("URL");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
}

@Test
public void Login() {
driver.findElement(By.id("username")).sendKeys("username");
driver.findElement(By.id("password")).sendKeys("password");
driver.findElement(By.id("login")).click();
}

@Test
public void Gettext() {
driver.findElement(By.id("psge_click")).click();
WebElement msg=driver.findElement(By.id("input_id"));
String expectedText=("sample text");
String text=msg.getText();
System.out.println(text);
Assert.assertEquals(text,expectedText);
}


I am using this code for ERROR Message, getting result, but input data not getting "empty" is displayed

Answer

Try as below to get the data from input field :-

WebElement msg=driver.findElement(By.id("input_id"));
String text = msg.getAttribute("value");

Hope it will help you....:)

Comments