无法与Selenium找到元素以接受隐私弹出

发布于 2025-02-13 19:17:12 字数 715 浏览 2 评论 0原文

我正在使用GURU99的Selenium测试一个演示网站(在Firefox上),每当我启动应用程序时,我都会被“管理您的隐私”弹出。我以其他方式解决了该问题,例如在Incognito中设置个人资料或启动浏览器,但我也想通过接受隐私弹出来做到这一点。

我编写了以下代码,以查找使用CSS选择器(或XPATH)的元素,然后单击它,但是我得到一个例外,没有任何具有#save的元素。

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

这是 site ,访问它时应该弹出。

还添加屏幕截图弹出并检查工具

为什么它不起作用?我应该重定向到窗口之类的警报吗?我不知道如何,弹出隐私是此页面的一部分。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一指流沙 2025-02-20 19:17:12

您必须切换到一个框架。
参见 https://www.guru9999.com/handling-iframes-iframes-iframes-iframes-selenium.html

代码:

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();
    }

}

输出:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文