-2

I'm trying to click on the ok button which is present on the alert/reminder but not able to handle alerts in the salesforce by usual methods.

This is the XPath:

driver.findElement(By.xpath("//div[@class='active cMA_Product_Category_Details']//div[@role='alertdialog']//button[text()='OK']")).click;

And also tried

driver.switchto.alert.accept;

Any idea how to handle this?

Please find the below attached screenshots for your reference. This is a salesforce alert.

alert image

alea
  • 980
  • 1
  • 13
  • 18

3 Answers3

0

To click on the OK button on a reminder alert in Salesforce using Selenium, you need to first switch the driver to the Alert interface. Once the alert is displayed you can use the following code:

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

This code snippet will select the alert box by using the switchTo() method, and then the accept() method is used to click on the OK button.

You can integrate this code into your Selenium script to handle the reminder alerts in Salesforce.

Anup Sahoo
  • 19
  • 3
0

For your problem, may be due to Selenium is too quick and tries to accept an alert that has not yet been opened by the browser.

So adding an explicit wait will be able to solve your problem.

WebDriverWait wait = new WebDriverWait(driver, 5);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert();
alert.accept()
  • actually this is a salesforce application and these methods are not working – JAYA CHANDRA N.B. May 29 '23 at 06:26
  • getting this error org.openqa.selenium.TimeoutException: Expected condition failed: waiting for alert to be present (tried for 5 second(s) with 500 milliseconds interval) Build info: version: '4.9.1', revision: 'eb2032df7f' – JAYA CHANDRA N.B. May 29 '23 at 06:34
0

Actually i found a solution for the issue

so alert.accept will not for work this type of salesforce alerts

we have to use xpath or css selector to find the element and then click on ok.