Home > Net >  How can I auto accept cookies pop windows Automation Java
How can I auto accept cookies pop windows Automation Java

Time:10-18

What is the wrong in my code , why auto click doesn't work in accept cookies button. This website using angular application.

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.paulhammant.ngwebdriver.ByAngular;
import com.paulhammant.ngwebdriver.NgWebDriver;

public class NewClass {

public static void main(String[] args) {
    
    System.setProperty("webdriver.chrome.driver",
            "C:\\Users\\Hp\\Downloads\\chromedriver_win32\\chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
                    NgWebDriver ngWebDriver = new NgWebDriver(driver);
                    ngWebDriver.waitForAngularRequestsToFinish();
        driver.get("https://visa.vfsglobal.com/ind/en/deu/login/");
        ngWebDriver = new NgWebDriver((JavascriptExecutor) driver);
        ngWebDriver.waitForAngularRequestsToFinish();           
        driver.findElement(ByAngular.options("onetrust-accept-btn-handler")).click();
 }

}

Attached Image here Just want to auto click this pop up

CodePudding user response:

As per my knowledge there is no way to auto accept cookies using ChromeOptions you need to find the element and click.

driver = new ChromeDriver();
driver.get("https://visa.vfsglobal.com/ind/en/deu/login/");
new WebDriverWait(driver, 30).until(ExpectedConditions.elementToBeClickable(By.id("onetrust-accept-btn-handler"))).click();

CodePudding user response:

When you use this line

driver.findElement(ByAngular.options("onetrust-accept-btn-handler")).click();

it will try to find the element immediately, often resulting in errors. Cause web element/elements have not been rendered properly.

This is main reason we should opt for Explicit waits, implemented by WebDriverWait.

They allow your code to halt program execution, or freeze the thread, until the condition you pass it resolves. The condition is called with a certain frequency until the timeout of the wait is elapsed. This means that for as long as the condition returns a falsy value, it will keep trying and waiting.

Code :

Webdriver driver = new ChromeDriver();
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.id("onetrust-accept-btn-handler"))).click();
  • Related