在启动URL之前在selenium Web驱动程序中提示用户输入

时间:2017-05-09 13:32:53

标签: java selenium selenium-webdriver selenium-iedriver

我正在尝试将用户输入存储到变量中,并且我想将该输入用于我的程序。

这是我的代码,代码是要求用户输入但不加载URL。它只是启动了驱动程序。请有人纠正我。

当前行为:

  1. 启动驱动程序(IE显示消息“这是wendriver服务器的初始启动页面”
  2. 要求提示。我在提示中输入了我的输入,然后单击“确定”。 多数民众赞成......在那段代码没有被执行之后。请帮帮我
  3. 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; 
                }
        }
    
        }
    

2 个答案:

答案 0 :(得分:0)

如果您需要从提示中获取输入(即URL),则可以使用Java Swing中的JOptionPane showInputDialog()方法。

代码段:

 String URL =JOptionPane.showInputDialog(null,"Enter URL");  

答案 1 :(得分:0)

尝试以下代码;它应该符合你的目的:

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) 
           { 

           }
   }

}