简体   繁体   English

无法使用 Selenium 定位元素以接受隐私弹出窗口

[英]Unable to locate element with Selenium to accept Privacy pop up

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.我正在使用来自 guru99 的 selenium 测试一个演示站点(在 firefox 上),每当我启动应用程序时,我都会被“管理您的隐私”弹出窗口打乱。 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.我编写了以下代码以使用 css 选择器(或 xpath)查找元素,然后单击它,但我得到一个异常,即不存在带有 #save 的此类元素。

    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.我不知道如何,因为隐私弹出是此页面的一部分。

There is a frame you have to switch to.您必须切换到一个框架。 See https://www.guru99.com/handling-iframes-selenium.htmlhttps://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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM