java sendkeys 错误键应该是一个字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18072725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java sendkeys Error keys should be a string
提问by adom
I'm getting an error when trying to pass in a username and password to a form field using sendKeys
. Below is my User Class followed by my test class. Does anyone know why the application is not passing a string?
尝试将用户名和密码传递到使用sendKeys
. 下面是我的用户类,然后是我的测试类。有谁知道为什么应用程序不传递字符串?
org.openqa.selenium.WebDriverException: unknown error: keys should be a string
org.openqa.selenium.WebDriverException:未知错误:键应该是字符串
public class User {
public static String username;
public static String password;
public User() {
this.username = "username";
this.password = "password";
}
public String getUsername(){
return username;
}
public String getPassword(){
return password;
}
}
@Test
public void starWebDriver() {
driver.get(domainURL.getURL());
WebElement userInputBox, passInputBox;
userInputBox = driver.findElement(By.xpath("//input[@name='email']"));
passInputBox = driver.findElement(By.xpath("//input[@name='password']"));
System.out.println("before sending keys");
userInputBox.sendKeys(User.username);
}
回答by John
You're accessing static properties that are never initialized (null) because the constructor is never called.
您正在访问从未初始化(空)的静态属性,因为从未调用构造函数。
You can either set the static properties directly or take out the static context and initialize a User in your test.
您可以直接设置静态属性,也可以取出静态上下文并在测试中初始化用户。
Ex.
前任。
public class User {
public String username;
public String password;
public User() {
this.username = "username";
this.password = "password";
}
public String getUsername(){
return username;
}
public String getPassword(){
return password;
}
}
@Test
public void starWebDriver() {
User user = new User();
driver.get(domainURL.getURL());
...
userInputBox.sendKeys(user.username);
}
回答by cegprakash
use
用
userInputBox.sendKeys(String.valueOf(User.username));