[Python] How to Save a Webpage as an .mhtml File for Nodriver

Discussão em 'Python' iniciado por Stack, Março 29, 2025.

  1. Stack

    Stack Membro Participativo

    In Selenium WebDriver, there is a function to capture the webpage as an MHTML file:

    execute_cdp_cmd('Page.captureSnapshot', {})

    However, I was unable to apply this code to my nodriver setup. Below is my code:

    import asyncio
    import nodriver as uc

    async def main():
    browser = await uc.start()
    page = await browser.get('https://www.nowsecure.nl')

    page2 = await browser.get('https://twitter.com', new_tab=True)
    res = page2.execute_cdp_cmd('Page.captureSnapshot', {})

    with open('test.mhtml', 'w', newline='') as f:
    f.write(res['data'])

    if __name__ == '__main__':
    uc.loop().run_until_complete(main())


    Let me know if you need further adjustments!

    Continue reading...

Compartilhe esta Página