使用Selenium Java无法切换role="dialog"模态框,无法点击Cookie弹窗
问题描述
打开ALDI在线商店页面后,无法点击Cookie弹窗,未发现iframe或alert窗口,尝试使用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.chrome.ChromeDriver; 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
- 调用alert方法报错:
org.openqa.selenium.NoAlertPresentException: no such alert
解决方法
1. 修复CSS选择器格式
你使用的属性选择器语法错误,正确的CSS属性选择器需要用方括号包裹。修改两处选择器:
- 原弹窗选择器:
By.cssSelector("data-testid='uc-default-wall'")→ 修改为By.cssSelector("[data-testid='uc-default-wall']") - 原按钮选择器:
By.cssSelector("data-testid='uc-accept-all-button'")→ 修改为By.cssSelector("[data-testid='uc-accept-all-button']")
2. 移除不必要的窗口切换逻辑
Cookie弹窗是页面内的普通模态元素,并非独立浏览器窗口,不需要调用getWindowHandles和窗口切换操作,直接定位元素即可。
3. 优化后的完整代码
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.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; 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(); WebDriverWait 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"); // 等待Cookie弹窗加载并点击接受按钮 wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[data-testid='uc-default-wall']"))); WebElement acceptBtn = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("[data-testid='uc-accept-all-button']"))); acceptBtn.click(); // 等待登录菜单可交互后点击 WebElement loginMenu = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("[data-attr-value='My Account']"))); loginMenu.click(); driver.quit(); } }
4. 关键说明
- 该Cookie弹窗既不是alert也不是iframe,属于页面内的普通DOM元素,直接通过CSS属性选择器定位即可。
- 使用
elementToBeClickable等待元素可交互后再点击,避免因元素未完全渲染导致的点击失败。
内容的提问来源于stack exchange,提问作者Leder
相关产品推荐
相关产品推荐

