Handling Alerts Selenium Webdriver

0 votes

I used alert method in the below code”

    try {
        Alert alert = webDriver.switchTo().alert();
         alert.getText();
                     log().info("Alert detected: {}" + alert.getText());
        alert.accept();
    } catch (Exception e) {
    }

When there is no alert on the current state, it waits for a specific amount of time until the timeout is reached, then throws an exception.How to do it in better way?

Apr 9, 2018 in Selenium by GandalfDwhite
• 1,320 points
136 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

You can use Explicit Wait from here https://www.seleniumhq.org/docs/04_webdriver_advanced.jsp#explicit-and-implicit-waits

public void checkAlert() {
    try {
        WebDriverWait wait = new WebDriverWait(driver, 2);
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        alert.accept();
    } catch (Exception e) {
                  }
}
answered Apr 9, 2018 by Damon Salvatore
• 5,250 points

Related Questions In Selenium

0 votes
1 answer

Handling JavaScript Alert window in Selenium WebDriver

You should try using waits for alerts ...READ MORE

answered Apr 10, 2018 in Selenium by ghost
• 1,790 points
581 views
0 votes
1 answer

Handling calendar popup using Selenium WebDriver

You are selecting wrongly your elements in ...READ MORE

answered Apr 17, 2018 in Selenium by Shubham
• 12,150 points
953 views
0 votes
1 answer

Handling print dialog in Selenium WebDriver

In Selenium WebDriver we are not able ...READ MORE

answered May 11, 2018 in Selenium by Meci Matt
• 9,400 points
618 views
0 votes
1 answer

Handling selenium webdriver popups

If you want to handle the most ...READ MORE

answered May 14, 2018 in Selenium by king_kenny
• 3,590 points
72 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,030 points
60 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
428 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
47 views
0 votes
1 answer

Double click a record in a grid using Selenium webdriver

Try Actions class to perform this Actions action ...READ MORE

answered Mar 29, 2018 in Selenium by Damon Salvatore
• 5,250 points
586 views
0 votes
1 answer

How to use Actions class in Selenium Webdriver?

In seleniun webdriver it is not mandatory ...READ MORE

answered Apr 4, 2018 in Selenium by Damon Salvatore
• 5,250 points
1,839 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.