首页 > 解决方案 > 如何使用硒自动化单击警报上的是否按钮(而不是确定/取消)?

问题描述

我正在尝试使用Selenium (java)单击警报弹出消息上的Yes/No按钮。我知道我们有 accept() 函数来点击任何警报的Ok按钮,但在这种情况下这不起作用。

我尝试了以下代码:

Alert alert = driver.switchTo().alert();
alert.accept();

这是警报消息的 HTML 代码:

<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
 <div class="ui-dialog-buttonset">
 <button type="button" class="ui-button ui-widget ui-state-default ui-corner- 
 all ui-button-text-only" role="button" aria-disabled="false">
  <span class="ui-button-text">Yes</span>
 </button>
 <button type="button" class="ui-button ui-widget ui-state-default ui-corner- 
 all ui-button-text-only" role="button" aria-disabled="false">
  <span class="ui-button-text">No</span>
 </button>
</div>
</div>

请帮忙!

标签: javaseleniumautomated-testsalert

解决方案


可以简单地单击按钮 。(无需切换到警报):

代码 :

new WebDriverWait(driver,10).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Yes']/parent::button"))).click();

推荐阅读