简体   繁体   中英

how to execute javascript prompt in selenium webdriver and wait for accept input

Can you help me solve my problem that 1.how to execute javascript prompt in selenium webdriver 2.after web driver wait until that user input and accept

after use this value to my code .

thanks advanced .

blow code .

 if (driver instanceof JavascriptExecutor) {
     ((JavascriptExecutor) driver).executeScript(""
            + "var aa =prompt('Please enter your name', 'Harry Potter');");

     Alert javascriptAlert = driver.switchTo().alert();
     driver.switchTo().alert().sendKeys("Helllo");
     javascriptAlert.accept();
        System.out.println(javascriptAlert.getText()); // Get text on alert box


}`

In the current revision of Selenium (2.47.0) you will be unable to do this. Selenium will throw an error if it sees an unexpected JavaScript dialogue. This means that as soon as you trigger some JavaScript that will make a popup appear an error will be thrown.

Yes, it will fail the script with the exception. See, here you are trying to access "text on the confirmation alert" using "javascriptAlert.getText()". However just in previous line you killed the "confirmation alert" using accept method.

The better way will be, like to keep the text in a variable and then print. Somehow like -

-- Previous code --
    driver.switchTo().alert().sendKeys("Helllo");
    string valueTextBox = javascriptAlert.getText();  
    javascriptAlert.accept();
    System.out.println(valueTextBox); 

Try this and let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM