简体   繁体   English

如何在Selenium Webdriver中执行javascript提示并等待接受输入

[英]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 您能帮我解决以下问题吗:1.如何在Selenium Webdriver中执行javascript提示2.Web驱动程序等到该用户输入并接受后

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(2.47.0)中,您将无法执行此操作。 Selenium will throw an error if it sees an unexpected JavaScript dialogue. 如果Selenium遇到意外的JavaScript对话,将抛出错误。 This means that as soon as you trigger some JavaScript that will make a popup appear an error will be thrown. 这意味着,一旦触发一些使弹出窗口出现的JavaScript,就会引发错误。

Yes, it will fail the script with the exception. 是的,它将使脚本失败,并带有异常。 See, here you are trying to access "text on the confirmation alert" using "javascriptAlert.getText()". 请参阅,此处您尝试使用“ 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. 试试这个,让我知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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