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
277 views

1 answer to this question.

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,510 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
902 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
• 13,300 points
1,563 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,420 points
1,457 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,650 points
124 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,110 points
967 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
1,127 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
869 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,510 points
1,141 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,510 points
2,506 views