在selenium java中:无法切换到页面上显示的模式角色=“dialog”

问题描述 投票:0回答:1

我在这里打开页面:

我无法单击 cookie 对话框!

我也看不到 iframe 或警报窗口。我尝试了

getWindowHandles
但没有成功。

这是我的硒测试:

package org.leder;


import dev.failsafe.internal.util.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.bidi.browsingcontext.BrowsingContext;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.Iterator;
import java.util.Set;

public class MainTest {

    public static final String ALDI_ONLINESHOP_GUTES_FÜR_ALLE = "ALDI ONLINESHOP - Gutes für alle";

    @Test
    public void loginLogoutTest() {
        WebDriver driver = new ChromeDriver();

        Wait<WebDriver> wait = new WebDriverWait(driver, Duration.ofSeconds(15));

        driver.get("https://aldi-onlineshop.de");

        String title = driver.getTitle();

        System.out.println(title);

        Assert.isTrue(title.equals(ALDI_ONLINESHOP_GUTES_FÜR_ALLE), "Title is valid");

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("data-testid='uc-default-wall'")));


        String parent = driver.getWindowHandle();

        Set<String> s = driver.getWindowHandles();


        // Now iterate using Iterator
        Iterator<String> I1 = s.iterator();

        driver.switchTo().window(I1.next());


        WebElement submitButton = driver.findElement(By.cssSelector("data-testid='uc-accept-all-button'"));


        submitButton.click();

        //switch to the parent window
        driver.switchTo().window(parent);

        WebElement loginMenu = driver.findElement(By.cssSelector("[data-attr-value='My Account']"));

        loginMenu.click();



        driver.quit();
    }
}

我期望选择模式对话框并按下按钮。我在模式对话框的 css 值上收到错误:

org.openqa.selenium.JavascriptException: javascript error: {"status":32,"value":"An invalid or illegal selector was specified"}
  (Session info: chrome=123.0.6312.122)
Build info: version: '4.20.0', revision: '866c76ca80'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '6.6.26-1-MANJARO', java.version: '17.0.10'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [4ffbffb982d379cb84581e3add5c448d, findElement {value=data-testid='uc-default-wall', using=css selector}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 123.0.6312.122, chrome: {chromedriverVersion: 123.0.6312.122 (31f8248cdd9..., userDataDir: /tmp/.org.chromium.Chromium...}, fedcm:accounts: true, goog:chromeOptions: {debuggerAddress: localhost:40857}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: linux, proxy: Proxy(), se:cdp: ws://localhost:40857/devtoo..., se:cdpVersion: 123.0.6312.122, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true}
Session ID: 4ffbffb982d379cb84581e3add5c448d

还有这个

driver.switchTo().alert().accept();

给出了这个

org.openqa.selenium.NoAlertPresentException: no such alert

java selenium-webdriver css-selectors
1个回答
0
投票

问题是您的两个 CSS 选择器无效。这就是错误的原因,例如

data-testid='uc-default-wall'
data-testid='uc-accept-all-button'

应加括号,例如

[data-testid='uc-default-wall']
[data-testid='uc-accept-all-button']

您的最后一个 CSS 选择器是有效的语法。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.