sir, we have one scenario where, for the first time, we need to dismiss the alert and for the second time we need to accept the alert. how can we handle such situation, here?
@Mukeshotwani2 ай бұрын
Hi Mate, Today I will be uploading video on alerts.
@Mukeshotwani2 ай бұрын
Here we go import com.microsoft.playwright.*; public class HandleMultipleDialogs { public static void main(String[] args) { try (Playwright playwright = Playwright.create()) { Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(200)); Page page = browser.newPage(); page.navigate("the-internet.herokuapp.com/javascript_alerts"); // Maintain a counter to track the number of dialogs final int[] dialogCount = {0}; // Add a listener for the dialog event page.onDialog(dialog -> { dialogCount[0]++; // Increment the counter if (dialogCount[0] == 1) { // First dialog: Dismiss the alert System.out.println("First dialog dismissed: " + dialog.message()); dialog.dismiss(); } else if (dialogCount[0] == 2) { // Second dialog: Accept the alert System.out.println("Second dialog accepted: " + dialog.message()); dialog.accept(); } }); // Trigger the first JS Alert (this will be dismissed) page.locator("//button[normalize-space()='Click for JS Prompt']").click(); // Trigger the second JS Alert (this will be accepted) page.locator("//button[normalize-space()='Click for JS Alert']").click(); // Close the browser after actions browser.close(); } } }