Search code examples
javaseleniumselenium-webdriverselenium-iedriver

Prompting user input in selenium web driver before launching URL


I am trying to take user input then stored into the variable and i want to use that input into my program .

Here is my code , code is asking for user input but not loading the URL . It is just initiating the driver. Please someone correct me .

Current behavior:

  1. Initiating the driver (IE shows the message " This is the Initial start page for wendriver server"
  2. Asking for prompt .I gave my input in the prompt and click OK. thats it .. after that code is not getting executed. Please help me

enter image description here

    public class app{   

    public static void main(String[] args) throws Throwable
    {

    System.setProperty("webdriver.ie.driver", "C:\\Automation\\IEDriverServer.exe");
    WebDriver driver = new InternetExplorerDriver();
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("window.promptResponse=prompt('Please enter the USER ID')");

    if(isAlertPresent(driver)) {
        // switch to alert
        Alert alert = driver.switchTo().alert();

        // sleep to allow user to input text
        Thread.sleep(10000);

        // this doesn't seem to work 
        alert.accept();
        String ID = (String) js.executeScript("return window.promptResponse");


    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);


    driver.get("my application URL");
    driver.findElement(By.name("USERID")).sendKeys("username");
    driver.findElement(By.name("user_pwd")).sendKeys("mypwd");
    driver.findElement(By.name("submit")).submit();
    .......
    ......
    // some more code which is doing my application fucntionality
    .......
    ......
    ........

    private static boolean isAlertPresent(WebDriver driver) {
         try 
            { 
                driver.switchTo().alert(); 
                return true; 
            }   // try 
            catch (NoAlertPresentException Ex) 
            { 
                return false; 
            }
    }

    }

Solution

  • Try following code; it should serve your purpose:

    public class app{   
    
        public static void main(String[] args) throws Throwable
        {
    
        System.setProperty("webdriver.ie.driver", "C:\\Automation\\IEDriverServer.exe");
        WebDriver driver = new InternetExplorerDriver();
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.promptResponse=prompt('Please enter the USER ID')");
    
        isAlertPresent(driver);
    
        String ID = (String) js.executeScript("return window.promptResponse");
    
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
    
        driver.get("my application URL");
        driver.findElement(By.name("USERID")).sendKeys("username");
        driver.findElement(By.name("user_pwd")).sendKeys("mypwd");
        driver.findElement(By.name("submit")).submit();
        }
    
        private static void isAlertPresent(WebDriver driver) {
            try 
               { 
                   driver.switchTo().alert(); 
                   driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  // even though not needed
    
                   isAlertPresent(driver);
               }   // try 
               catch (NoAlertPresentException Ex) 
               { 
    
               }
       }
    
    }