如何使用 selenium 自动化单击警报上的是否按钮(而不是 Ok/Cancel)?
How to click on Yes No button on an alert (rather than Ok/Cancel) using selenium automation?
我正在尝试使用 Selenium (java)[=24 单击警报弹出消息上的 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>
请帮忙!
不是Javascript alert
。
使用这个 xpath 来点击那个按钮
"//span[normalize-space()='Yes']/.."
您只需在 是 按钮 上单击。 (无需切换到警报):
代码:
new WebDriverWait(driver,10).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Yes']/parent::button"))).click();
您可以使用参数化函数在Yes/No上单击。
如果您想单击“是”,请使用:ClickYesNoButton("Yes");
对于否,将函数调用为:ClickYesNoButton("No");
代码如下:
public void ClickYesNoButton(String yesOrno){
String myXpath = "//span[text()='XXXX']";
driver.findElement(By.xpath(myXpath.replace("XXXX", yesOrno))).click();
}
我正在尝试使用 Selenium (java)[=24 单击警报弹出消息上的 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>
请帮忙!
不是Javascript alert
。
使用这个 xpath 来点击那个按钮
"//span[normalize-space()='Yes']/.."
您只需在 是 按钮 上单击。 (无需切换到警报):
代码:
new WebDriverWait(driver,10).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Yes']/parent::button"))).click();
您可以使用参数化函数在Yes/No上单击。
如果您想单击“是”,请使用:ClickYesNoButton("Yes");
对于否,将函数调用为:ClickYesNoButton("No");
代码如下:
public void ClickYesNoButton(String yesOrno){
String myXpath = "//span[text()='XXXX']";
driver.findElement(By.xpath(myXpath.replace("XXXX", yesOrno))).click();
}