【问题标题】:Selenium WebDriver: Size of the text fieldSelenium WebDriver:文本字段的大小
【发布时间】:2013-06-11 23:05:40
【问题描述】:

我在 Selenium WebDriver 中遇到了一个基本问题:如何测试表单中文本字段的容量?我使用了 .sendKeys(String),然后尝试使用 .getText() 取回文本,但它不起作用。它检索一个空字符串。 那么如何测试文本字段的大小呢?如何比较我的输入和实际数据? 这是我的代码:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.testng.Assert;
public class main {
public static void main(String [] args){
WebDriver driver= new FirefoxDriver();
driver.navigate().to("http://energy-telecom.org/ServiceReview/quote.php");
String valid="0123456789";
WebElement element=driver.findElement(By.name("firstName"));
element.sendKeys(valid);
System.out.println("input: "+element.getText());
//Assert.assertEquals(element.getText(), valid, "Valid_TEXT");
}

} 我的输出:

input:  

我知道我可以使用 .getAttributes,但我很好奇我可以检索这个值。 显然,最后一行被注释了,因为它给出了一个例外。 谢谢你。 PS:请不要以这种形式提交任何东西:D

【问题讨论】:

  • 您的代码中的Valid_TEXT 是什么?你给这个变量赋值了吗?另外请粘贴您得到的异常,这将有助于调试
  • 当然,我忘记发布输出了;我更新了我的问题
  • @GeorgeRevkov:您不应该将getText() 用于输入字段,而应使用getAttributes(),因为您看到的文本就是值。 “但我很好奇,拿回你在这个领域里投入的东西”是什么意思?
  • @GeorgeRevkov:然后使用getAttributes()。您是在谈论文本框中有多少个字符或它的实际大小(长度/宽度)?
  • 我的意思是:检索这个输入是真的吗?谢谢,如果使用 getAttribute("value") 就可以了。

标签: selenium webdriver textfield gettext capacity


【解决方案1】:

对于输入,文本是值,所以你可以试试

System.out.println("input: "+ element.getAttribute("value"));

【讨论】:

    猜你喜欢
    • 2013-10-08
    • 2017-12-01
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 2013-10-09
    • 1970-01-01
    相关资源
    最近更新 更多