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

P. H. Nabila :

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!

cruisepandey :

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();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=103744&siteId=1