Get text from textbox in Selenium WebDriver
we can use element.getAttribute("value")
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class getTextFromTextBox {
@Test
public void testGetText() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in");
driver.manage().window().maximize();
Thread.sleep(2000);
driver.findElement(By.cssSelector(".gsfi")).sendKeys("selenium testing");
driver.findElement(By.name("btnG")).click();
Thread.sleep(3000);
String searchKeyword = driver.findElement(By.cssSelector(".gsfi")).getAttribute("value");
System.out.println("The searched keyword is "+ searchKeyword);
driver.close();
}
@Test
public void testGetTextFromTextBox() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("https://in.yahoo.com/");
driver.manage().window().maximize();
Thread.sleep(2000);
driver.findElement(By.id("UHSearchBox")).sendKeys("selenium is automation test suite");
driver.findElement(By.id("UHSearchWeb")).click();
Thread.sleep(2000);
String textValue = driver.findElement(By.id("yschsp")).getAttribute("value");
System.out.println("The searched keyword is "+ textValue);
driver.close();
}
}
Output :
The searched keyword is selenium is automation test suite
The searched keyword is selenium testing
we can use element.getAttribute("value")
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class getTextFromTextBox {
@Test
public void testGetText() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in");
driver.manage().window().maximize();
Thread.sleep(2000);
driver.findElement(By.cssSelector(".gsfi")).sendKeys("selenium testing");
driver.findElement(By.name("btnG")).click();
Thread.sleep(3000);
String searchKeyword = driver.findElement(By.cssSelector(".gsfi")).getAttribute("value");
System.out.println("The searched keyword is "+ searchKeyword);
driver.close();
}
@Test
public void testGetTextFromTextBox() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("https://in.yahoo.com/");
driver.manage().window().maximize();
Thread.sleep(2000);
driver.findElement(By.id("UHSearchBox")).sendKeys("selenium is automation test suite");
driver.findElement(By.id("UHSearchWeb")).click();
Thread.sleep(2000);
String textValue = driver.findElement(By.id("yschsp")).getAttribute("value");
System.out.println("The searched keyword is "+ textValue);
driver.close();
}
}
Output :
The searched keyword is selenium is automation test suite
The searched keyword is selenium testing
No comments:
Post a Comment