java - 如何使用硒自动化单击警报上的是否按钮(而不是确定/取消)?
问题描述
我正在尝试使用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>
请帮忙!
解决方案
您可以简单地单击是按钮 。(无需切换到警报):
代码 :
new WebDriverWait(driver,10).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Yes']/parent::button"))).click();
推荐阅读
- r - 匹配一个变量后,仅将一个变量从一个 R data.table 复制到另一个
- python - 如何根据另一列值更改列值?
- mongodb - 在 Spring Data MongoDB 中保存文档时如何从默认值中删除字段
- c# - 水晶报表数据源更新问题
- image-processing - 图像每个像素可以有不同的位和不同的位深度吗
- firebase - 我在使用 firebase 的 android studio 中遇到问题
- azure - Blob 存储帐户的 Azure SAS 令牌不起作用
- jupyter-notebook - 我可以像 Google Colab 一样使用 AWS Sagemaker 吗?
- python - 通过 Selenium 导航下载课程列表
- python - 处理 Brownie FundMe Chainlink 演示和与合约交互的脚本在 Rinkeby 上工作,但不在 Ganache