2

I tried driver.switchTo().alert().accept(); but still i am unable to accept the alert message. please let me know the code to accept below message.

enclosed screenshot of message and XML

enter image description here

2

3 Answers 3

2

As far as I can tell from your screenshot, you do not face an alert. The "Cookie control" is just a HTML element on your website and is styled to overlay the website.

So you just need to click on the "Cancel" button like on any other WebElement.

Maybe there are some challanges you will face. Like when does this popup appears? Maybe you need to implement a logic like the following, when you visit the website the very first time:

WAIT FOR THE POPUP TO APPEAR
IF THE POPUP IS PRESENT:
    CLICK THE CANCEL BUTTON

You can use the WebDriverWaits to hande this: https://www.seleniumhq.org/docs/04_webdriver_advanced.jsp

Another option would be to set a cookie before you visit the website. Maybe the popup does not appears when a certain cookie is set. This way you do not need to interact with the popup at all, which saves you execution time and makes your tests more stable.

4
  • thanks for the info. Any chance can you write the code for above logic. I have used WebDriver Waits logic as well but still i was getting error
    – Arjun
    Nov 21, 2018 at 11:38
  • @Arjun What exactly was the error? Maybe it is more helpful than just providing the code here.
    – Twaldigas
    Nov 26, 2018 at 8:15
  • Errror message is : error message Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable:
    – Arjun
    Nov 26, 2018 at 9:35
  • @Arjun This exception will be thrown, when the element was not found after the set timeout. You can ignore this by using another wait class. Example: FluentWait<WebDriver> fluentWait = new FluentWait<>(driver) .withTimeout(Duration.ofSeconds(10)) .pollingEvery(Duration.ofMillis(200)) .ignoring(NoSuchElementException.class);
    – Twaldigas
    Nov 27, 2018 at 10:22
0

A common reason of failure it might be that webdriver is trying to click to accept an alert before is present in the DOM or clickable, using a fluent wait should works:

public void acceptAlert() {
        try {
            new WebDriverWait(driverProvider.get(), 100).until(ExpectedConditions.alertIsPresent());
            switchTo().alert().accept();
        } catch (Exception e) {
               e.printstacktrace());
        }
    }

Although what i see in the developer console it doesn't looks like an alert but a form button what you want to click instead, have you tried something like this? :

WebElement inputButton = driver.findElement(By.xpath("//input[contains(@class, 'btnCancelCookie')]"));
    inputButton.click();
0

But for your case, it seems it is not alert popup. It seems like cookies just an HTML element from the web page.

I am recommending the below code to handle the cookies:

for (Cookie cook : driver.manage().getCookies()) {
   String writeup = cook.getName();
   driver.manage().deleteCookie(cook); 
}

This is for handling alert window to accept it.

Alert alert = driver.switchTo().alert();
WebUtil.waitFor(500);
alert.accept();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.