How to get typed text from a textbox by using Selenium Webdriver

+1 vote
How to get typed text from a textbox by using Selenium Webdriver?
Jun 25, 2019 in Selenium by Ashmita
38,145 views

4 answers to this question.

+2 votes
Best answer

Hey Ashmita, to get the typed text from a textbox, you can use getAttribute(“value”) method by passing arg as value. Value attribute stores the typed text of the textbox element. See the following example to know better: 

String typedText = driver.findElement(By.xpath("xpath_textbox")).getAttribute("value"));
answered Jun 25, 2019 by Abha
• 28,140 points

selected Dec 31, 2019 by Kalgi
thanks for your valueable info and keep on doing like this ..it is really usefull
Hello, can you please upvote the answer if it has helped you?

Thanks :)
Thanks a lot!
+1 vote

var val = driver.findElement(webdriver.By.name("AccToken")).getText();
javascript:
  1. String text = (String) jsExecutor.executeScript("return document.getElementById('txtfirstName').value");  

 

answered Sep 6, 2020 by Sri
• 3,190 points
0 votes

The getText() method is for retrieving a text node between element tags for example:

<p>Something</p>

getText() will return "Something"

In a textbox typed text goes into the value attribute so you can try something like:

findElement(By.id("someid")).getAttribute("value");

ComboBox is a bit different. But if you're using the Select object you can use the method:

Select selectItem = new Select(findElement(By.id("someid")));
selectItem.getFirstSelectedOption().getText();

For further understanding, you can refer to the Selenium Training.

answered Dec 16, 2020 by Gitika
• 65,910 points
0 votes

We can get the entered text from a textbox in Selenium webdriver. To obtain the value attribute of an element in the html document, we have to use the getAttribute() method. Then the value is passed as a parameter to the method.

Let us consider a textbox where we entered some text and then want to get the entered text.

If we spy on the element, we will find that there is no value attribute for this element in the html code.

image

After entering text inside that field, we can get the entered text by using the getAttribute() method.

Example

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class GetValAttribute{
   public static void main(String[] args) {
   System.setProperty("webdriver.chrome.driver","C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://www.tutorialspoint.com/index.htm";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
      // identify element
      WebElement l = driver.findElement(By.id("gsc-i-id1"));
      // enter texts
      l.sendKeys("Selenium");
      // get value attribute with getAttribute()
      String val = l.getAttribute("value");
      System.out.println("Entered text is: " + val);
      driver.quit()
   }
}
answered Dec 16, 2020 by Rajiv
• 8,910 points

Related Questions In Selenium

0 votes
2 answers

How to get the text from a website using selenium?

driver.findElement(By.cssSelector("p")).getText() or  IWebElement element = Browser.GetElementByCssSelector("div.loginbox p"); string text = ...READ MORE

answered Sep 6, 2020 in Selenium by Sri
• 3,190 points
25,076 views
0 votes
1 answer
0 votes
2 answers

How to get a page name using Selenium Webdriver (C#)?

String pageTitlte=driver.getTitle(); READ MORE

answered Sep 6, 2020 in Selenium by Sri
• 3,190 points
2,132 views
0 votes
1 answer

How to extract text from a web page using selenium and save it as a text file?

Hello Isha, you can checkout this code ...READ MORE

answered May 7, 2019 in Selenium by Anvi
• 14,150 points
33,103 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
12,618 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,572 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
6,629 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,519 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP