我正在尝试使用 Selenium (java) 在警报弹出消息上单击是/否 按钮。我知道我们有 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>
请帮忙!
最佳答案
您只需点击是按钮。 (无需切换到警报):
代码:
new WebDriverWait(driver,10).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Yes']/parent::button"))).click();
关于java - 如何使用 selenium 自动化单击警报上的是否按钮(而不是确定/取消)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51691934/