Skip to content

[Bug] Windows 11 Edge 访客/无痕模式下通过 CDP 接管后,page.context.cookies() 获取的 cookies 不完整,且 page.request.get() 无法正常获取需要验证的资源 #40923

@E5C8F

Description

@E5C8F

Version

1.58.0

Steps to reproduce

执行文件:


import asyncio
from playwright.async_api import async_playwright, expect, Playwright, Browser, Page
import tempfile
import subprocess
import playwright._impl._errors

async def browser_init(p: Playwright, 浏览器路径: str = fr"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe", 主机: str = 'localhost', 端口: int = 9222, 无头模式: bool = False, 超时: int = 5) -> Browser:
    数据目录: str = tempfile.mkdtemp()
    browser_init = False
    for _ in range(超时):
        try:
            browser = await p.chromium.connect_over_cdp(f"http://{主机}:{端口}")
            return browser
        except playwright._impl._errors.Error as e:
            if not browser_init:

                if 浏览器路径.endswith('chrome.exe'):
                    命令 = fr'{浏览器路径} --remote-debugging-address=:: --remote-debugging-port={端口} --user-data-dir="{数据目录}" --guest'
                    if 无头模式:命令 +=' --headless=new'
                elif 浏览器路径.endswith('msedge.exe'):
                    命令 = fr'{浏览器路径} --remote-debugging-address=:: --remote-debugging-port={端口} --user-data-dir="{数据目录}" --guest'
                    if 无头模式:命令 +=' --headless=new'
                elif 浏览器路径.endswith('firefox.exe'):
                    命令 = fr'{浏览器路径} --start-debugger-server [::]:{端口} -profile "{数据目录}" --no-remote'
                    if 无头模式:命令 +=' --headless'
                else:
                    命令 = fr'{浏览器路径} --remote-debugging-address=:: --remote-debugging-port={端口} --user-data-dir="{数据目录}" --guest'
                    if 无头模式:命令 +=' --headless=new'
                    print(f'你可能传入了非主流浏览器路径:{浏览器路径}, 默认使用 chrome 浏览器 启动方式, 这可能会导致异常')

                subprocess.Popen(命令)
                browser_init = True
        await asyncio.sleep(1)




async def main():

    async with async_playwright() as p:
        browser = await browser_init(p, 浏览器路径 = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe")
        page = browser.contexts[0].pages[0]

        await page.goto("https://www.qidian.com/")
        await asyncio.sleep(5)

        cookies1 = await page.context.cookies()

        client = await page.context.new_cdp_session(page)
        cookies2 = (await client.send('Network.getAllCookies')).get('cookies')
        client.detach()

        print(cookies1)
        print(cookies2)

        cookies3 = await page.evaluate('() => document.cookie')
        print(cookies3)
        print(await (await page.request.get('https://www.qidian.com/all/')).text())

        await browser.close()


        
if __name__ == '__main__':
    asyncio.run(main())
    # 测试设备详情:
    # 版本	Windows 11 专业工作站版
    # 版本号	25H2
    # 处理器	AMD Ryzen 7 5700U with Radeon Graphics (1.80 GHz)
    # 机带 RAM	16.0 GB (15.3 GB 可用)
    # 图形卡	AMD Radeon(TM) Graphics (497 MB)
    # 存储	已使用 477 GB 中的 230 GB
    # 系统类型	64 位操作系统, 基于 x64 的处理器
    # PS D:\Program Files\dev> python -V
    # Python 3.13.12
    # PS D:\Program Files\dev> pip show playwright
    # Name: playwright
    # Version: 1.58.0
    # Summary: A high-level API to automate web browsers
    # Home-page: https://github.com/Microsoft/playwright-python
    # Author: Microsoft Corporation
    # Author-email: 
    # License-Expression: Apache-2.0
    # Location: d:\Program Files\dev\python-3.13.12-amd64\Lib\site-packages
    # Requires: greenlet, pyee
    # Required-by: 
    #测试结果:
    # []
    # [{'name': 'x-waf-captcha-referer', 'value': '', 'domain': 'www.qidian.com', 'path': '/', 'expires': 1779272547.766072, 'size': 21, 'httpOnly': False, 'secure': False, 'session': False, 'priority': 'Medium', 'sourceScheme': 'Secure', 'sourcePort': 443}, {'name': 'w_tsfp', 'value': 'ltv2UU8E3ewC6mwF46vuk0KsETojfTAhkA5sXqNmeJ94Q7ErU5mC2IR9vM//OHDa5Mxnt9jMsoszd3qAUdEsfxYWQcSZdowXkB/Gy99yicxUQ0k5VYnWSwEaJ+5z7WRGfWJdJEXmiGkqJ9JGxeA1j1EOuyV8yf53XvFqL5kXjB0ZufzCkpxuDW3HlFWQRzaZciVfKr/c9OtwraxQ9z/c5Vv7LFt0A6hewgfHg31dWzox6wOpaPsYd0W/Kdz3HKlw7ibwsyz1HIWur1Fkpk526UpkU4vqimqXOnQyMhd5NgDn0eVnPvO5PPR2uzAQCvhNS1wUrQYQs+poqhBNWSi/M3SGAK4u5gUGQeAU', 'domain': 'www.qidian.com', 'path': '/', 'expires': 1779272608, 'size': 362, 'httpOnly': False, 'secure': False, 'session': False, 'priority': 'Medium', 'sourceScheme': 'Secure', 'sourcePort': 443}, {'name': 'newstatisticUUID', 'value': '1779272484_1866987316', 'domain': '.qidian.com', 'path': '/', 'expires': 1810808489.312202, 'size': 37, 'httpOnly': False, 'secure': False, 'session': False, 'priority': 'Medium', 'sourceScheme': 'Secure', 'sourcePort': 443}, {'name': 'HMACCOUNT_BFESS', 'value': 'D65226F91375895E', 'domain': '.hm.baidu.com', 'path': '/', 'expires': 1813832490.140512, 'size': 31, 'httpOnly': False, 'secure': True, 'session': False, 'sameSite': 'None', 'priority': 'Medium', 'sourceScheme': 'Secure', 'sourcePort': 443}]
    # 测试结论:
    # 在通过cdp接管处于访客模式的edge浏览器的页面时(无痕模式下也一样),通过await page.context.cookies()获取的cookies是严重缺失的,但通过cdp获取的cookies却是完整的,希望修复此bug
    # 影响:page.request.get无法正确获取需要验证的资源,可能还有其它潜在影响
    # 注意:每次测试前务必关闭现有cdp浏览器,避免影响测试

测试结果

通过await page.context.cookies()获取的cookies严重缺失(对比而言)(继而导致page.request.get无法正确获取某些资源)
控制台显示:

PS D:\Program Files\dev> & "d:/Program Files/dev/python-3.13.12-amd64/python.exe" "d:/Program Files/dev/自动化模块.py"
(node:10392) [DEP0169] DeprecationWarning: `url.parse()` behavior is not standardized and prone to errors that have security implications. Use the WHATWG URL API instead. CVEs are not issued for `url.parse()` vulnerabilities.
(Use `node --trace-deprecation ...` to show where the warning was created)
d:\Program Files\dev\自动化模块.py:53: RuntimeWarning: coroutine 'CDPSession.detach' was never awaited
  client.detach()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
[]
[{'name': 'x-waf-captcha-referer', 'value': '', 'domain': 'www.qidian.com', 'path': '/', 'expires': 1779274296.280051, 'size': 21, 'httpOnly': False, 'secure': False, 'session': False, 'priority': 'Medium', 'sourceScheme': 'Secure', 'sourcePort': 443}, {'name': 'newstatisticUUID', 'value': '1779274233_1066250769', 'domain': '.qidian.com', 'path': '/', 'expires': 1810810237.925502, 'size': 37, 'httpOnly': False, 'secure': False, 'session': False, 'priority': 'Medium', 'sourceScheme': 'Secure', 'sourcePort': 443}, {'name': 'HMACCOUNT_BFESS', 'value': '5EEDFDA0EB44FBD9', 'domain': '.hm.baidu.com', 'path': '/', 'expires': 1813834238.845122, 'size': 31, 'httpOnly': False, 'secure': True, 'session': False, 'sameSite': 'None', 'priority': 'Medium', 'sourceScheme': 'Secure', 'sourcePort': 443}, {'name': 'Hm_lvt_f00f67093ce2f38f215010b699629083', 'value': '1779274239', 'domain': '.qidian.com', 'path': '/', 'expires': 1810810239, 'size': 49, 'httpOnly': False, 'secure': False, 'session': False, 'priority': 'Medium', 'sourceScheme': 'Secure', 'sourcePort': 443}, {'name': 'Hm_lpvt_f00f67093ce2f38f215010b699629083', 'value': '1779274239', 'domain': '.qidian.com', 'path': '/', 'expires': -1, 'size': 50, 'httpOnly': False, 'secure': False, 'session': True, 'priority': 'Medium', 'sourceScheme': 'Secure', 'sourcePort': 443}, {'name': 'HMACCOUNT', 'value': '5EEDFDA0EB44FBD9', 'domain': '.qidian.com', 'path': '/', 'expires': -1, 'size': 25, 'httpOnly': False, 'secure': False, 'session': True, 'priority': 'Medium', 'sourceScheme': 'Secure', 'sourcePort': 443}, {'name': '_csrfToken', 'value': '1UzmlU2eBYMCORDSK8WAp6EadPzoGxaNJeEdIavi', 'domain': '.qidian.com', 'path': '/', 'expires': -1, 'size': 50, 'httpOnly': False, 'secure': False, 'session': True, 'priority': 'Medium', 'sourceScheme': 'Secure', 'sourcePort': 443}, {'name': 'traffic_utm_referer', 'value': '', 'domain': '.qidian.com', 'path': '/', 'expires': 1779292799, 'size': 19, 'httpOnly': False, 'secure': False, 'session': False, 'priority': 'Medium', 'sourceScheme': 'Secure', 'sourcePort': 443}, {'name': 'fu', 'value': '663955286', 'domain': '.qidian.com', 'path': '/', 'expires': 1813834240.449611, 'size': 11, 'httpOnly': False, 'secure': False, 'session': False, 'priority': 'Medium', 'sourceScheme': 'Secure', 'sourcePort': 443}, {'name': 'e1', 'value': '%7B%22l6%22%3A%22%22%2C%22l7%22%3A%22%22%2C%22l1%22%3A3%2C%22l3%22%3A%22%22%2C%22pid%22%3A%22qd_p_qidian%22%2C%22eid%22%3A%22qd_A1001%22%7D', 'domain': '.qidian.com', 'path': '/', 'expires': 1781866243, 'size': 141, 'httpOnly': False, 'secure': False, 'session': False, 'priority': 'Medium', 'sourceScheme': 'Secure', 'sourcePort': 443}, {'name': 'e2', 'value': '%7B%22l6%22%3A%22%22%2C%22l7%22%3A%22%22%2C%22pid%22%3A%22qd_p_qidian%22%2C%22eid%22%3A%22qd_A1008%22%2C%22l1%22%3A3%7D', 'domain': '.qidian.com', 'path': '/', 'expires': 1781866243, 'size': 121, 'httpOnly': False, 'secure': False, 'session': False, 'priority': 'Medium', 'sourceScheme': 'Secure', 'sourcePort': 443}, {'name': 'w_tsfp', 'value': 'ltvuV0MF2utBvS0Q7qPrk0+sEj0mdjk4h0wpEaR0f5thQLErU5mC2IR9usnzNXzf5sxnvd7DsZoyJTLYCJI3dwNBQsnAIN9AiQ2Ql9Vw2ooTBEE2GZ7cUVdOcL0m5GETfXhCNxS00jA8eIUd379yilkMsyN1zap3TO14fstJ019E6KDQmI5uDW3HlFWQRzaLbjcMcuqPr6g18L5a5T/etFn6JVIhBOxE1RbE1npNX3AksBm/deoLZxSscsimSqA=', 'domain': 'www.qidian.com', 'path': '/', 'expires': 1779274260, 'size': 262, 'httpOnly': False, 'secure': False, 'session': False, 'priority': 'Medium', 'sourceScheme': 'Secure', 'sourcePort': 443}]
x-waf-captcha-referer=; newstatisticUUID=1779274233_1066250769; Hm_lvt_f00f67093ce2f38f215010b699629083=1779274239; Hm_lpvt_f00f67093ce2f38f215010b699629083=1779274239; HMACCOUNT=5EEDFDA0EB44FBD9; _csrfToken=1UzmlU2eBYMCORDSK8WAp6EadPzoGxaNJeEdIavi; traffic_utm_referer=; fu=663955286; e1=%7B%22l6%22%3A%22%22%2C%22l7%22%3A%22%22%2C%22l1%22%3A3%2C%22l3%22%3A%22%22%2C%22pid%22%3A%22qd_p_qidian%22%2C%22eid%22%3A%22qd_A1001%22%7D; e2=%7B%22l6%22%3A%22%22%2C%22l7%22%3A%22%22%2C%22pid%22%3A%22qd_p_qidian%22%2C%22eid%22%3A%22qd_A1008%22%2C%22l1%22%3A3%7D; w_tsfp=ltvuV0MF2utBvS0Q7qPrk0+sEj0mdjk4h0wpEaR0f5thQLErU5mC2IR9usnzNXzf5sxnvd7DsZoyJTLYCJI3dwNBQsnAIN9AiQ2Ql9Vw2ooTBEE2GZ7cUVdOcL0m5GETfXhCNxS00jA8eIUd379yilkMsyN1zap3TO14fstJ019E6KDQmI5uDW3HlFWQRzaLbjcMcuqPr6g18L5a5T/etFn6JVIhBOxE1RbE1npNX3AksBm/deoLZxSscsimSqA=

Additional context

此bug可能有其它潜在影响,我暂未测试

Environment

# 测试设备详情:
    # 版本	Windows 11 专业工作站版
    # 版本号	25H2
    # 处理器	AMD Ryzen 7 5700U with Radeon Graphics (1.80 GHz)
    # 机带 RAM	16.0 GB (15.3 GB 可用)
    # 图形卡	AMD Radeon(TM) Graphics (497 MB)
    # 存储	已使用 477 GB 中的 230 GB
    # 系统类型	64 位操作系统, 基于 x64 的处理器
    # PS D:\Program Files\dev> python -V
    # Python 3.13.12
    # PS D:\Program Files\dev> pip show playwright
    # Name: playwright
    # Version: 1.58.0
    # Summary: A high-level API to automate web browsers
    # Home-page: https://github.com/Microsoft/playwright-python
    # Author: Microsoft Corporation
    # Author-email: 
    # License-Expression: Apache-2.0
    # Location: d:\Program Files\dev\python-3.13.12-amd64\Lib\site-packages
    # Requires: greenlet, pyee
    # Required-by:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions