Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

343
Views
How can I auto accept cookies pop windows Automation Java

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

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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();
over 4 years ago · Santiago Trujillo Report

0

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();
over 4 years ago · Santiago Trujillo Report

0

Please add wait functionality before entering username and password,

if pop up interrupt while entering, username and password will not be proper.

In my case, i created a common method wait and click,

   dh.wait_for_the_element_then_do(By.xpath("//button[text()='Accept All Cookies']"), "click", "", "cookies popo up");
   dh.wait_for_the_element_then_do(By.xpath("//input[@tcp-auto='input-username']"),"send", username, "login username");
   dh.wait_for_the_element_then_do(By.xpath("//input[@tcp-auto='input-password']"),"send", password, "login password");
   dh.wait_for_the_element_then_do(By.xpath("//span[text()='Sign in']"),"click", "", "signin button");
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!