eclipse Java/Selenium WebDriver:如何获取用户输入并将其插入到文本字段中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15685069/
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/Selenium WebDriver: How to get user input and insert it in to a text field?
提问by dhruveonmars
At the moment in Eclipse, I have the following code:
目前在 Eclipse 中,我有以下代码:
driver.findElement(By.name("LoginText1")).clear();
System.out.println("Username: ");
Scanner scan1 = new Scanner(System.in);
String input1 = scan1.nextLine();
System.out.println(input1);
and was just wondering, instead of it simply returning the user's input in to the console, is there a way to get it to insert it in to the text field. I want to insert it into a field which has the name LoginText1. This is for website testing with Selenium WebDriver.
I am new to Java and all help is greatly appreciated.
只是想知道,而不是简单地将用户的输入返回到控制台,有没有办法让它将其插入到文本字段中。我想将它插入到名称为LoginText1的字段中。这是用于使用 Selenium WebDriver 进行网站测试。
我是 Java 新手,非常感谢所有帮助。
回答by 0x6C38
After seeing the edits on your question, I'm afraid I can only link you to Selenium's online documentation:
在看到对您问题的编辑后,恐怕我只能将您链接到 Selenium 的在线文档:
回答by WilliamShatner
You would simply do something like this
你会简单地做这样的事情
LoginText1.setText(input1);
I suggest looking into JTextField documentation and you would have quickly found examples and solutions to your problem.
我建议查看 JTextField 文档,您会很快找到问题的示例和解决方案。
I also suggest looking into variable naming conventions (LoginText1 should start with a lowercase).
我还建议查看变量命名约定(LoginText1 应以小写开头)。
EDIT
编辑
After seeing your most recent edit, you are wanting to fill in a form from a website using selenium webdriver. A post at StackOverflow shows just that using the sendKeys()method.
在看到您最近的编辑后,您想使用 selenium webdriver 从网站填写表单。StackOverflow 上的一篇文章展示了使用该sendKeys()方法。
回答by sachin
in this scenario user is passing the password manually through console.
在这种情况下,用户通过控制台手动传递密码。
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String password;
System.out.println("Please Enter the user password :: ");
password= br.readLine();
driver.findElement(By.id("pwd")).sendKeys("password");
回答by SivaPalepu
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Index of the sheet: ");
int index= br.read();
pass ur Variable(index) wherever its Required.

