Reputation: 45
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
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
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
Reputation: 6064
It's not Javascript alert
.
Use this xpath to click that button
"//span[normalize-space()='Yes']/.."
Upvotes: 0