You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium Java报错:Chrome v92识别到Frame仍抛出NoSuchFrameException切换失败

问题原因

Chrome v92无法切换指定Frame,同操作Firefox可正常执行的核心原因有两点:

  • Chrome v92不会实时刷新动态插入iframe的索引计数,Firefox会同步更新DOM树的索引,使用frame(0)、frame(1)的索引定位方式在Chrome下不稳定
  • 触发下注coupon后,boa-overlay-iframe是异步渲染节点,切换回defaultContent后直接执行切换iframe操作时,节点还未完全插入DOM树,Chrome驱动无法识别到对应帧
修复方案

1. 替换索引定位为属性定位,增加显式等待

放弃使用iframe索引定位,改用className属性匹配目标帧,同时增加显式等待确保帧已加载完成,修改后的代码如下:

@Test
void auto_015_5() {
    int bet = 7;        
    driver.navigate().to("https://boademo.com/"); 
    // 第一个iframe也替换为属性定位,避免索引不稳定问题
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.className("boa-iframe")));
    
    List < WebElement > counters = driver.findElements(By.xpath("//div[@class='counter']"));
    counters.get(0).click();     
    List < WebElement > marketOptions = driver.findElements(By.xpath("//div[@class='au-target market-option row nowrap can-place-bets']"));
    for (int i = 0; i <= (bet * 3) - 1; i += 3) {
        WebElement currentOption = marketOptions.get(i);
        JavascriptExecutor je = (JavascriptExecutor) driver;
        if (i % 6 == 0 && i >= 6) {
            je.executeScript("arguments[0].scrollIntoView(true);", marketOptions.get(i - 6));
        }
        currentOption.click();
    }
    
    driver.switchTo().defaultContent();
    // 等待下注coupon的iframe加载完成后自动切换
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.className("boa-overlay-iframe")));
}

2. 适配Chrome v92的特殊配置

如果修改后仍然报错,是Chrome v92的站点隔离特性导致动态跨域iframe识别异常,启动ChromeDriver时增加以下启动参数即可:

ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-features=IsolateOrigins,site-per-process");
WebDriver driver = new ChromeDriver(options);

内容的提问来源于stack exchange,提问作者Nick Borg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 16:18:03