如何在 Selenium Webdriver 中获取元素的文本() [英] How to gettext() of an element in Selenium Webdriver

查看:56
本文介绍了如何在 Selenium Webdriver 中获取元素的文本()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 ID 查找文本框.我需要获取文本框中已经存在的内容.为此,我使用了 gettext() 方法,但它返回了 ID 值.

I am finding a textbox by its ID. I need to get the content which is already there inside the text box. For that I am using the gettext() method, but it is returning the ID value.

文本框中的内容是:Santhosh

我得到的输出是 = [[FirefoxDriver: firefox on XP (c0079327-7063-4908-b20a-a606b95830cb)] ->id: ctl00_ContentPlaceHolder1_txtName]

代码如下

WebElement TxtBoxContent = driver.findElement(By.id(WebelementID));
TxtBoxContent.getText();
System.out.println("Printing " + TxtBoxContent);

结果

Printing [[FirefoxDriver: firefox on XP (c0079327-7063-4908-b20a-a606b95830cb)] -> id: ctl00_ContentPlaceHolder1_txtName]

推荐答案

您需要打印 getText() 的结果.您当前正在打印对象 TxtBoxContent.

You need to print the result of the getText(). You're currently printing the object TxtBoxContent.

getText() 只会获取元素的内部文本.要获取该值,您需要使用 getAttribute().

getText() will only get the inner text of an element. To get the value, you need to use getAttribute().

WebElement TxtBoxContent = driver.findElement(By.id(WebelementID));
System.out.println("Printing " + TxtBoxContent.getAttribute("value"));

这篇关于如何在 Selenium Webdriver 中获取元素的文本()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆