如何使用Selenium关闭Cookie通知栏。

huangapple go评论102阅读模式
英文:

How to close the cookie banner using Selenium

问题

以下是翻译好的代码部分:

public void closeCookieNotification() {
    try {
        // 检查是否存在Cookie通知元素
        WebElement cookieNotification = driver.findElement(By.xpath("//*[@id=\"onetrust-accept-btn-handler\"]"));

        // 如果存在,关闭Cookie通知
        if (cookieNotification.isDisplayed()) {
            WebElement closeButton = driver.findElement(By.xpath("//*[@id=\"onetrust-accept-btn-handler\"]"));
            closeButton.click();
        }
    } catch (Exception e) {
        // Cookie通知不存在或关闭时出现错误,忽略
        System.out.println(e.getMessage());
    }
}
英文:

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());
        }
    }

答案1

得分: 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));
英文:

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:

<!-- begin snippet: java hide: false console: true babel: false -->

<!-- language: lang-java -->

WebDriverWait wdwait = new WebDriverWait(driver, 10);

driver.get(&quot;https://www.allianz.de/recht-und-eigentum/hausratversicherung/&quot;);
WebElement consent = wdwait.until(ExpectedConditions.visibilityOfElementLocated(By.id(&quot;onetrust-accept-btn-handler&quot;)));
consent.click();
wdwait.until(ExpectedConditions.invisibilityOf(consent));

<!-- end snippet -->

答案2

得分: 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(&quot;https://hausrat.allianz.de/&quot;);
    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.id(&quot;onetrust-accept-btn-handler&quot;))).click();
    
  • Using cssSelector:

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

    driver.get(&quot;https://hausrat.allianz.de/&quot;);
    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath(&quot;//button[@id=&#39;onetrust-accept-btn-handler&#39;]&quot;))).click();
    

huangapple
  • 本文由 发表于 2023年8月5日 02:02:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定