【问题标题】:Is there any way to load cookies in headless chrome (Selenium)?有没有办法在无头铬(Selenium)中加载cookie?
【发布时间】:2021-10-01 02:10:51
【问题描述】:

我想加载所有的 cookie:

options.add_argument('user-data-dir=C:/Users/user/AppData/Local/Google/Chrome/User Data')

但是当我尝试在--headless 中运行它时,它不起作用。登录不上网站,有什么办法吗?

我尝试了什么:

我这样做是为了使用谷歌获取我当前的位置,我无法发送登录密钥,因为它说:

浏览器或应用程序可能不安全

【问题讨论】:

    标签: python selenium web-scraping cookies google-chrome-headless


    【解决方案1】:

    来自 selenium 文档:https://selenium-python.readthedocs.io/navigating.html#cookies

    在转到本教程的下一部分之前,您可能有兴趣了解如何使用 cookie。首先,您需要位于 cookie 有效的域中:

    请记住,管理 Cookie 会变得复杂。您的 cookie 会随着与网站的交互而改变,这意味着您保存的 cookie 将变得陈旧。我使用的适用于我的解决方案总是在您完成 selenium 会话后写出您的 cookie。

    这里是保存和设置 cookie 的一个非常简单的版本:

    import os
    import json
    import time
    from selenium import webdriver
    
    # Assuming the webdriver is next to this file and no chromedriver.exe. I am on linux
    CHROME_DRIVER_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'chromedriver')
    
    driver = webdriver.Chrome(executable_path=CHROME_DRIVER_PATH)
    
    save_cookies = True
    cookies_path = 'valid_json_list_of_your_cookies_path'
    
    # Navigate to the domain that you need to save/write cookies on
    driver.get('the_place_you_want_to_login_at')
    
    if save_cookies:
        # Save cookies mode just means we want to save the cookies and do nothing else.
        # Two minutes of sleep to allow you to login to the website
        time.sleep(120)
        # You should be transitioned to the the landing page after logged in by now
        cookies_list = driver.get_cookies()
    
        # Write out the cookies while you are logged in
        with open(cookies_path, 'w') as file_path:
            json.dump(cookies_list, file_path, indent=2, sort_keys=True)
    
        driver.quit()
    
    else:
        # Not saving cookies mode assumes we have saved them and can read them in
        with open(cookies_path, 'r') as file_path:
            cookies_list = json.loads(file_path.read())
    
        # Once on that domain, start adding cookies into the browser
        for cookie in cookies_list:
            # If domain is left in, then in the browser domain gets transformed to f'.{domain}'
            cookie.pop('domain', None)
            driver.add_cookie(cookie)
    
        # Based on how the page works, it may reload and move you forward after cookies are set
        # OR
        # You may have to call driver.refresh()
    

    【讨论】:

    • 但是如何将这些 cookie 写入文件,我尝试使用 pickle,但似乎不起作用
    猜你喜欢
    • 2019-12-12
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    相关资源
    最近更新 更多