如何在Selenium Webdriver中获取元素的文本() [英] How to gettext() of an element in 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 gettext()
method but it is returning the ID value
文本框中的内容是: Santhosh
Content in text box is: Santhosh
我得到的输出是= [[FirefoxDriver:XP上的firefox(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屋!