简体   繁体   中英

How to click on Yes No button on an alert (rather than Ok/Cancel) using selenium automation?

I am trying to click on the Yes/No button on an alert popup message using Selenium (java) . I know we have accept() functions to click on the Ok buttons of any alert, but that doesn't work in this case.

I tried the below codes:

Alert alert = driver.switchTo().alert();
alert.accept();

This is the HTML code of the alert message:

<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>

Please help!

You can simply click on Yes button . (No need to switch to alert ) :

Code :

new WebDriverWait(driver,10).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Yes']/parent::button"))).click();

You can use a parameterized function to Click on Yes/No . In case you want to click on Yes, use: ClickYesNoButton("Yes"); For No, call function as : ClickYesNoButton("No");

Code below:

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

It's not Javascript alert .

Use this xpath to click that button

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM