Home > Enterprise >  Unable to locate element with Selenium to accept Privacy pop up
Unable to locate element with Selenium to accept Privacy pop up

Time:07-08

I am testing a demo site (on firefox) with selenium from guru99 and whenever i start the application i get disrupted by "Manage Your Privacy" pop up. I've solved the issue in other manners like, setting a profile or starting browser in incognito, but i also want to do it by accepting the privacy pop up.

I wrote the following code to find the element with css selector(or xpath) and then click it, but i get an exception that no such element with #save exists.

    WebElement accept = driverFirefox.findElement(By.cssSelector("#save"));
    waiter.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("#save")));
    accept.click();

This is the site, you should get a pop up when you visit it.

Also adding a screenshot Pop up and inspect tool

Why is it not working? Should i redirect to an alert like window? I dont know how since, privacy pop up is a part of this page.

CodePudding user response:

There is a frame you have to switch to. See https://www.guru99.com/handling-iframes-selenium.html

Code:

package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import selenium.ChromeDriverSetup;

public class Spoomba extends ChromeDriverSetup {

    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = startChromeDriver(); // wrapped driver with implicitlyWait 10 seconds
        driver.get("https://demo.guru99.com/test/newtours/");
        driver.switchTo().frame("gdpr-consent-notice");
        WebElement acceptButton = driver.findElement(By.id("save"));
        System.out.println("displayed: "   acceptButton.isDisplayed());
        System.out.println("enabled: "   acceptButton.isEnabled());
        System.out.println("text: "   acceptButton.getText());
        acceptButton.click();
        System.out.println("frame gone: "   (driver.findElements(By.id("gdpr-consent-notice")).size() == 0));
        driver.quit();
    }

}

Output:

Starting ChromeDriver 103.0.5060.53 (a1711811edd74ff1cf2150f36ffa3b0dae40b17f-refs/branch-heads/5060@{#853}) on port 9798
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Čvc 07, 2022 4:26:20 ODP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
displayed: true
enabled: true
text: Přijmout vše
frame gone: true
  • Related