如何使用硒自动化点击警报上的Yes No按钮(而不是Ok/Cancel)



我正试图使用硒(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();

您可以使用参数化函数来单击是/否。如果要单击"是",请使用:ClickYesNoButton("Yes");对于No,调用函数为:ClickYesNoButton("No");

下面的代码:

public void ClickYesNoButton(String yesOrno){
String myXpath =    "//span[text()='XXXX']";
driver.findElement(By.xpath(myXpath.replace("XXXX", yesOrno))).click();
}

它不是Javascript alert

使用此xpath单击按钮

"//span[normalize-space()='Yes']/.."

最新更新