0

There is a cookie on this page which i have tried to close using selenium java and testNG but not closing. The url is https://hausrat.allianz.de/

Below is my code:

public void closeCookieNotification() {//*[@id="onetrust-accept-btn-handler"]
        try {
            // Check if the cookie notification element is present
            WebElement cookieNotification = driver.findElement(By.xpath("//*[@id=\"onetrust-accept-btn-handler\"]"));

            // Close the cookie notification if present
            if (cookieNotification.isDisplayed()) {
                WebElement closeButton = driver.findElement(By.xpath("//*[@id=\"onetrust-accept-btn-handler\"]"));
                closeButton.click();
            }
        } catch (Exception e) {
            // Cookie notification not present or error occurred while closing, ignore
            System.out.println(e.getMessage());
        }
    }
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • you'll probably want to use a WebDriverWait for this. – pcalkins Aug 04 '23 at 18:58
  • Where do you think it is appropriate to WebdriverWait. Because this is in a class that is instantiated in the Test class so where do you will be ideal – Sheriff Sulemana Aug 04 '23 at 20:44
  • these types of prompts are usually populated via Javascript which means Selenium won't know to wait for them to appear. That's when you want to use a WebDriverWait when making a driver call to find an element. Most sites are pretty JS heavy these days, so as a general rule, always use a WebDriverWait for all your driver calls involving the DOM. The one exception being after a .get() call is used as Selenium will wait for the page to load in that case. (there's no harm in using it, as it will return just about immediately if the element is found.) – pcalkins Aug 04 '23 at 21:56

2 Answers2

1

You should use WebDriverWait in this case until consent modal is appeared. findElement is executed immediately, and consent is not appeared direct after site load.

You can try this code:

WebDriverWait wdwait = new WebDriverWait(driver, 10);

driver.get("https://www.allianz.de/recht-und-eigentum/hausratversicherung/");
WebElement consent = wdwait.until(ExpectedConditions.visibilityOfElementLocated(By.id("onetrust-accept-btn-handler")));
consent.click();
wdwait.until(ExpectedConditions.invisibilityOf(consent));
Yaroslavm
  • 1,762
  • 2
  • 7
  • 15
1

The element is a dynamic element so to click on the clickable element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:

  • Using id:

    driver.get("https://hausrat.allianz.de/");
    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.id("onetrust-accept-btn-handler"))).click();
    
  • Using cssSelector:

    driver.get("https://hausrat.allianz.de/");
    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button#onetrust-accept-btn-handler"))).click();
    
  • Using xpath:

    driver.get("https://hausrat.allianz.de/");
    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@id='onetrust-accept-btn-handler']"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352