31

I enter a value in TextBox or a Combobox, and would like to retrieve the value I have just entered. I see that Selenium Weblement method getText() doesn't retrieve the value, it seems the entered text doesn't get pushed into DOM.

Any solutions?

Null
  • 1,950
  • 9
  • 30
  • 33
Fazy
  • 573
  • 1
  • 5
  • 16

3 Answers3

60

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();
Michael
  • 3,093
  • 7
  • 39
  • 83
Bob Paulin
  • 1,088
  • 8
  • 13
  • 2
    Thanks bob, it works as expected. Why using Firebug the entered text is not getting updated in the value attribute ? – Fazy Dec 22 '12 at 09:06
  • Just to be clear, it has be to getAttribute("value"), lower case. Not "Value". Which is what i got wrong. – TK Tang Sep 05 '19 at 02:32
0

Try getValue if it is a Text Field or Dropdown box

String lastname=selenium.getValue("//*[@id='lastName']");
System.out.println(lastname);
Morvader
  • 2,317
  • 3
  • 31
  • 44
ChanGan
  • 4,254
  • 11
  • 74
  • 135
0

This is how we can fetch the text written in a textbox using Selenium + Python:

text = driver.find_element_by_xpath("Type_Xpath_Here").get_attribute('value')
print(text)
Amar Kumar
  • 2,392
  • 2
  • 25
  • 33