Selenium:如何提示用户输入并使用输入值?

我有一种情况(在selenium测试期间),用户将收到安全代码。 然后,用户必须输入安全代码才能继续。

我不太确定如何获得用户输入的值。 我浏览了selenium文档并想出了这个。 不幸的是,它并不常用。

JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.promptResponse=prompt('Please enter the security code')"); if(isAlertPresent()) { // switch to alert Alert alert = driver.switchTo().alert(); // sleep to allow user to input text Thread.sleep(10000); // this doesn't seem to work code = (String) js.executeScript("return window.promptResponse"); alert.accept(); 

有人能指出我正确的方向吗?

在能够存储和使用该值之前,您似乎必须首先接受并关闭提示

  alert.accept(); code = (String) js.executeScript("return window.promptResponse");