P. H. Nabila
P. H. Nabila

Reputation: 45

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!

Upvotes: 3

Views: 5005

Answers (3)

HemSa
HemSa

Reputation: 176

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

Upvotes: 1

cruisepandey
cruisepandey

Reputation: 29362

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

Upvotes: 1

Rajagopalan
Rajagopalan

Reputation: 6064

It's not Javascript alert.

Use this xpath to click that button

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

Upvotes: 0

Related Questions