0

At the moment in Eclipse, I have the following code:

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.

dhruveonmars
  • 409
  • 1
  • 11
  • 24

4 Answers4

1

After seeing the edits on your question, I'm afraid I can only link you to Selenium's online documentation:

http://docs.seleniumhq.org/docs/

0x6C38
  • 6,796
  • 4
  • 35
  • 47
  • did you declare the textfield? – 0x6C38 Mar 28 '13 at 15:21
  • If you mean what you just changed, then no. I have done exactlty what you wrote: `LoginText1 = new JTextField(20); LoginText1.setText(input1);` But I still get the same error, but now in both places. – dhruveonmars Mar 28 '13 at 15:31
  • 1
    @D.Shah You need to really read into the basics of Java before jumping into Gui. Grab a book and start from the beginning. – WilliamShatner Mar 28 '13 at 15:39
  • I appreciate that, but I don't appear to have the time, and I think this is one of the only things I am having a problem with now. Also, I tried what you said, I didn't get the error, but it still doesn't enter the text in to the field. _Also, what would I need the JFrame for?_ – dhruveonmars Mar 28 '13 at 15:48
  • I added the JFrame and the rest, but when I run it, it simply gives me a Java Pop-up with the username I entered. It still doesn't enter the username in to the field. – dhruveonmars Mar 28 '13 at 15:58
  • The "Pop-up" is actually a jFrame that contains the jTextField with the username. I thought thats what you wanted? – 0x6C38 Mar 28 '13 at 16:03
  • No, I needed the text the user inputs to be inserted in to the text field. – dhruveonmars Mar 28 '13 at 16:05
  • Can you explain the difference between what should be happening and what actually happens? – 0x6C38 Mar 28 '13 at 16:08
  • _What is happening_: When I run it, in the Console it asks me for the username. After I enter it, there is a java pop-up which has the user input written in, and the header of what I put in "Your JFrame". _What I need_: For it to ask the user for the username or an input, and then after the user has input something, it inserts that in to the username text-field on the website. – dhruveonmars Mar 28 '13 at 16:14
  • @D.Shah That small edit you did not long ago changes the question entirely... You aren't asking how to fill in a textfield in java, you are asking how to fill in a textfield on a website via selenium-webdriver. You should have stated this from the beginning – WilliamShatner Mar 28 '13 at 16:19
  • Sorry for the confusion. So, would all the JFrame stuff be irrelevant to this or still be useful? – dhruveonmars Mar 28 '13 at 16:22
  • No. Go look at my edited answer. I strongly suggest before going further with web-automation that you sit down and learn some basic java. – WilliamShatner Mar 28 '13 at 16:23
1

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.

Here

I also suggest looking into variable naming conventions (LoginText1 should start with a lowercase).

Here

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.

Here is the link

Community
  • 1
  • 1
WilliamShatner
  • 926
  • 2
  • 12
  • 25
  • I added that, and also added `import java.awt.TextField;` but I get the error _Logintext1 cannot be resolved_. Also, I would change the name of the field if I could, but I don't have that sort of access. I am just supposed to make automated testing using Selenium for as many browsers as possible. – dhruveonmars Mar 28 '13 at 15:09
0

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");
sachin
  • 1
0

Pass your variable index wherever it is required.

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Index of the sheet: ");
int index = br.read();
VLAZ
  • 26,331
  • 9
  • 49
  • 67
SivaPalepu
  • 11
  • 1
  • 2