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);
Mykola
  • 3,343
  • 6
  • 23
  • 39
Joe
  • 147
  • 1
  • 4
  • 17
  • Possible duplicate of [Selenium: How to prompt user input and use the input value?](http://stackoverflow.com/questions/26600003/selenium-how-to-prompt-user-input-and-use-the-input-value) – ArcticLord Jan 08 '16 at 14:59
  • @ArcticLord ....but am still getting "unexpected alert open" error. – Joe Jan 08 '16 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);` – Joe Jan 08 '16 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 – Joe Jan 08 '16 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. – Johnny C Jan 15 '16 at 13:31
  • I suspect that you will find your answer in Q&A [Reading JavaScript variables using Selenium WebDriver](https://stackoverflow.com/questions/13994393/reading-javascript-variables-using-selenium-webdriver) – MikeJRamsey56 May 04 '17 at 22:38

0 Answers0