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

如何在Chrome Webdriver配置中允许第三方网站Cookie访问

Selenium Chrome无痕模式允许第三方Cookie配置方案

可以通过Chrome启动参数直接配置允许第三方Cookie,无需每次手动调整设置,配置仅对当前Webdriver启动的会话生效,不会修改本地Chrome默认设置,也不影响其他测试场景的无痕模式使用。

核心配置参数

只需给ChromeOptions添加如下启动参数即可:

  • --incognito:指定启动无痕浏览模式
  • --disable-features=ThirdPartyCookieBlocking:直接关闭Chrome的第三方Cookie拦截特性
  • 可选(适配旧版Chrome的SameSite规则拦截场景):--disable-features=SameSiteByDefaultCookies,CookiesWithoutSameSiteMustBeSecure

代码示例

Python 示例

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
# 启用无痕模式
chrome_options.add_argument("--incognito")
# 关闭第三方Cookie拦截
chrome_options.add_argument("--disable-features=ThirdPartyCookieBlocking")
# 旧版Chrome可额外添加下方参数关闭SameSite默认拦截规则
# chrome_options.add_argument("--disable-features=SameSiteByDefaultCookies,CookiesWithoutSameSiteMustBeSecure")

driver = webdriver.Chrome(options=chrome_options)
# 后续正常访问站点A即可,iframe中站点B的内容会正常加载

Java 示例

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class IframeTest {
    public static void main(String[] args) {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--incognito");
        chromeOptions.addArguments("--disable-features=ThirdPartyCookieBlocking");
        // 旧版Chrome可额外添加下方参数
        // chromeOptions.addArguments("--disable-features=SameSiteByDefaultCookies,CookiesWithoutSameSiteMustBeSecure");
        WebDriver driver = new ChromeDriver(chromeOptions);
    }
}

验证方法

Webdriver启动浏览器后,访问chrome://settings/cookies即可看到无痕模式下的第三方Cookie拦截已经关闭,无需手动操作。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 13:06:02