如何在Selenium Webdriver中获取元素的gettext()

时间:2014-02-28 06:35:22

标签: java selenium selenium-webdriver

我正在通过其ID找到一个文本框。我需要获取文本框中已有的内容。为此我使用gettext()方法,但它返回ID值

文本框中的内容是:Santhosh

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

代码在

之下

CODE:

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]

3 个答案:

答案 0 :(得分:9)

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

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

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

答案 1 :(得分:3)

在显示之前,您需要先将其存储在TypeError: Fetch argument [<tf.Tensor 'DynamicPartition_25:0' shape=(?, 2) dtype=float32>, <tf.Tensor 'DynamicPartition_25:1' shape=(?, 2) dtype=float32>, <tf.Tensor 'DynamicPartition_25:2' shape=(?, 2) dtype=float32>, <tf.Tensor 'DynamicPartition_25:3' shape=(?, 2) dtype=float32>] of [<tf.Tensor 'DynamicPartition_25:0' shape=(?, 2) dtype=float32>, <tf.Tensor 'DynamicPartition_25:1' shape=(?, 2) dtype=float32>, <tf.Tensor 'DynamicPartition_25:2' shape=(?, 2) dtype=float32>, <tf.Tensor 'DynamicPartition_25:3' shape=(?, 2) dtype=float32>] has invalid type <class 'list'>, must be a string or Tensor. (Can not convert a list into a Tensor or Operation.) 变量中:

String

答案 2 :(得分:0)

System.out.print(“Ans”+ str.getAttribute(“innerHTML”));