3

How do i store user input and print it out using JavascriptExecutor? ... Below is what i have tried so far. The prompt box works fine and takes user input. but i need to get what the user input and store it in String, so that i can use it in my code.

JavascriptExecutor javascript = (JavascriptExecutor) driver;
    String ans = (String) javascript.executeScript("prompt('How do you want to SIGN UP?\\n\\n (1) FACEBOOK\\n (2) GOOGLE\\n (3) LOCALLY');");
//Object ans = javascript.executeScript("prompt('How do you want to SIGN UP?\\n\\n (1) FACEBOOK\\n (2) GOOGLE\\n (3) LOCALLY');");
    Thread.sleep(10000);
    //String answer = (String) ans;
    System.out.println(ans);
6
  • Possible duplicate of Selenium: How to prompt user input and use the input value? Commented Jan 8, 2016 at 14:59
  • @ArcticLord ....but am still getting "unexpected alert open" error. Commented Jan 8, 2016 at 15:11
  • JavascriptExecutor javascript = (JavascriptExecutor) driver; javascript.executeScript("window.promptResponse=prompt('How do you want to SIGN UP?\\n\\n (1) FACEBOOK\\n (2) GOOGLE\\n (3) LOCALLY');"); Thread.sleep(5000); //Alert alert = null; //alert.accept(); String answer = (String) javascript.executeScript("return window.promptResponse"); //System.out.println(answer); Commented Jan 8, 2016 at 15:12
  • have tried this code several times but all to no avail..... please any help. i dont think its a duplicate as the previous post is not printing out anything. i keep getting a null pointer exception if i attempt to print out because of the cast Commented Jan 8, 2016 at 16:01
  • 1
    @Joe why do you want to do it via JsExecutor? Usually, the use case would be to click on a button in the page, get the JS alert, type in text then get its value. But this should be implemented in the actual page under test, not by your Webdriver script. By synthetically creating the prompt, you're trying to add manual user intervention into an automated script - is this really what you want? If you want to have control over what type of login to be executed, it can be done differently. Commented Jan 15, 2016 at 13:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.