英文:
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("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));
<!-- 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("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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论