如何使用硒自动化单击警报(而不是确定/取消)上的“是”按钮?

我正在尝试使用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>

请帮忙!


qq_花开花谢_0
浏览 109回答 3
3回答

胡子哥哥

您只需单击“是”&nbsp;按钮即可。(无需切换到警报):代码 :new&nbsp;WebDriverWait(driver,10).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Yes']/parent::button"))).click();

撒科打诨

不是Javascript alert。使用此 xpath 单击该按钮"//span[normalize-space()='Yes']/.."
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java