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()' doesnt retrieve the value, it seems the entered text doesn't get pushed into DOM.
我在TextBox或Combobox中输入一个值,并希望检索刚刚输入的值。我看到Selenium Weblement方法'getText()'没有检索值,似乎输入的文本没有被推入DOM。
Any Solutions ?
任何方案?
2 个解决方案
#1
43
The getText()
method is for retrieving a text node between element tags for example:
getText()方法用于检索元素标记之间的文本节点,例如:
<p>Something</p>
getText()
will return "Something"
getText()将返回“Something”
In a textbox typed text goes into the value attribute so you can try something like:
在文本框中,键入的文本会进入value属性,因此您可以尝试以下操作:
findElement(By.id("someid")).getAttribute("value");
ComboBox
is a bit different. But if you're using the Select
object you can use the method:
ComboBox有点不同。但是,如果您使用的是Select对象,则可以使用以下方法:
Select selectItem = new Select(findElement(By.id("someid")));
selectItem.getFirstSelectedOption().getText();
#2
1
Try getValue
if it is a Text Field or Dropdown box
如果是文本字段或下拉框,请尝试使用getValue
String lastname=selenium.getValue("//*[@id='lastName']");
System.out.println(lastname);
#1
43
The getText()
method is for retrieving a text node between element tags for example:
getText()方法用于检索元素标记之间的文本节点,例如:
<p>Something</p>
getText()
will return "Something"
getText()将返回“Something”
In a textbox typed text goes into the value attribute so you can try something like:
在文本框中,键入的文本会进入value属性,因此您可以尝试以下操作:
findElement(By.id("someid")).getAttribute("value");
ComboBox
is a bit different. But if you're using the Select
object you can use the method:
ComboBox有点不同。但是,如果您使用的是Select对象,则可以使用以下方法:
Select selectItem = new Select(findElement(By.id("someid")));
selectItem.getFirstSelectedOption().getText();
#2
1
Try getValue
if it is a Text Field or Dropdown box
如果是文本字段或下拉框,请尝试使用getValue
String lastname=selenium.getValue("//*[@id='lastName']");
System.out.println(lastname);