且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从 Selenium 中的文本框中获取输入的文本

更新时间:2023-02-23 22:59:09

getText() 方法用于检索元素标签之间的文本节点,例如:

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

<p>Something</p>

getText() 将返回Something"

getText() will return "Something"

在文本框中输入的文本进入 value 属性,因此您可以尝试以下操作:

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

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

ComboBox 有点不同.但是,如果您使用的是 Select 对象,则可以使用以下方法:

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();