Zac*_*yon 5 java selenium alert webdriver
有谁知道如何禁用它?或者如何从已自动接受的警报中获取文本?
这段代码需要工作,
driver.findElement(By.xpath("//button[text() = \"Edit\"]")).click();//causes page to alert() something
Alert alert = driver.switchTo().alert();
alert.accept();
return alert.getText();
Run Code Online (Sandbox Code Playgroud)
但反而给出了这个错误
No alert is present (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 2.14 seconds
Run Code Online (Sandbox Code Playgroud)
我正在使用FF 20和Selenium 2.32
就在前几天,我已经回答了类似的事情,所以它仍然很新鲜.您的代码失败的原因是,如果在处理代码时未显示警报,则它将大部分失败.
值得庆幸的是,来自Selenium WebDriver的人已经等待它了.对于您的代码就像这样简单:
String alertText = "";
WebDriverWait wait = new WebDriverWait(driver, 5);
// This will wait for a maximum of 5 seconds, everytime wait is used
driver.findElement(By.xpath("//button[text() = \"Edit\"]")).click();//causes page to alert() something
wait.until(ExpectedConditions.alertIsPresent());
// Before you try to switch to the so given alert, he needs to be present.
Alert alert = driver.switchTo().alert();
alertText = alert.getText();
alert.accept();
return alertText;
Run Code Online (Sandbox Code Playgroud)
您可以找到所有的API ExpectedConditions 在这里,如果你想这个方法后面的代码在这里.
此代码也解决了这个问题,因为您在关闭警报后无法返回alert.getText(),因此我会为您存储一个变量.
| 归档时间: |
|
| 查看次数: |
21844 次 |
| 最近记录: |