0

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

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

当前行为:

  1. 启动驱动程序(IE 显示消息“This is the Initial start page for wendriver server”
  2. 要求提示。我在提示中输入了我的输入,然后单击“确定”。就是这样..在那之后代码没有被执行。请帮我

在此处输入图像描述

    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; 
            }
    }

    }
4

2 回答 2

1

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

代码片段:

 String URL =JOptionPane.showInputDialog(null,"Enter URL");  
于 2017-05-09T14:43:45.847 回答
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) 
           { 

           }
   }

}
于 2017-05-09T14:43:46.927 回答