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

如何在Undetected Chromedriver中禁用DNS over HTTPS

问题:Undetected Chromedriver关闭安全DNS后JA3指纹无变化

我在项目中使用Undetected Chromedriver,想要关闭chrome://settings/security页面中的「使用安全DNS」选项。普通Selenium里用localState实验性选项可以完美实现,但Undetected Chromedriver会报错「invalid argument: unrecognized chrome option: localState」,推测是该驱动不支持这类实验性选项。

于是我尝试通过附加--user-data-dir参数,在指定目录下创建Local State文件并写入JSON配置的方式来设置。结果启动驱动后,chrome://settings/security页面里的安全DNS选项确实显示已关闭,但这个设置对JA3指纹完全没有影响。

示例代码

import os
import tempfile
import json
from functools import reduce
import undetected_chromedriver as uc
from selenium import webdriver

class ChromeWithPrefs(uc.Chrome):
    def __init__(self, *args, options=None, **kwargs):
        if options:
            user_data_dir = os.path.normpath(tempfile.mkdtemp())
            options.add_argument(f"--user-data-dir={user_data_dir}")
            self._handle_local(options, user_data_dir)
            self._handle_prefs(options, user_data_dir)
        super().__init__(*args, options=options, **kwargs)

        # 退出时删除user_data_dir
        self.keep_user_data_dir = False

    @staticmethod
    def _handle_prefs(options, user_data_dir):
        prefs = options.experimental_options.get("prefs")
        if prefs:
            # 将带点的键转换为嵌套字典
            def undot_key(key, value):
                if "." in key:
                    key, rest = key.split(".", 1)
                    value = undot_key(rest, value)
                return {key: value}

            # 处理所有带点的配置键
            undot_prefs = reduce(
                lambda d1, d2: {**d1, **d2},  # 合并字典
                (undot_key(key, value) for key, value in prefs.items()),
            )

            # 创建默认目录并写入Preferences文件
            default_dir = os.path.join(user_data_dir, "Default")
            os.mkdir(default_dir)

            prefs_file = os.path.join(default_dir, "Preferences")
            with open(prefs_file, encoding="latin1", mode="w") as f:
                json.dump(undot_prefs, f)

            # 删除实验性选项避免报错
            del options._experimental_options["prefs"]

    @staticmethod
    def _handle_local(options, user_data_dir):
        prefs = options.experimental_options.get("localState")
        if prefs:
            # 将带点的键转换为嵌套字典
            def undot_key(key, value):
                if "." in key:
                    key, rest = key.split(".", 1)
                    value = undot_key(rest, value)
                return {key: value}

            # 处理所有带点的配置键
            undot_prefs = reduce(
                lambda d1, d2: {**d1, **d2},  # 合并字典
                (undot_key(key, value) for key, value in prefs.items()),
            )

            # 创建Local State文件并写入配置
            prefs_file = os.path.join(user_data_dir, "Local State")

            with open(prefs_file, encoding="latin1", mode="w") as f:
                json.dump(undot_prefs, f)

            # 删除实验性选项避免报错
            del options._experimental_options["localState"]

options_chrome = webdriver.ChromeOptions()
options_chrome.add_argument("--start-maximized")
local_state = {
    "dns_over_https.mode": "off"
}
options_chrome.add_experimental_option('localState', local_state)

driver = ChromeWithPrefs(
    options=options_chrome, suppress_welcome=True
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 15:42:26