Search code examples
pythonwebkitgtkpygobject

webkit2 webview: how to store cookies and reuse it again?


I am trying to get cookies to work. It says cookies are enable if I load a cookie test page, but it won't remember logins.

I am posting my code below, thanks in advance.

    context = WebKit2.WebContext.get_default()
    sm = context.get_security_manager()

    self.cookies = context.get_cookie_manager()
    self.manager = WebKit2.UserContentManager()
    self.webview = 
    WebKit2.WebView.new_with_user_content_manager(self.manager)
    self.add(self.webview)
    self.settings = self.webview.get_settings()

    cookiesPath = '/tmp/cookies.txt'
    storage = WebKit2.CookiePersistentStorage.TEXT
    policy = WebKit2.CookieAcceptPolicy.ALWAYS

    self.cookies.set_accept_policy(policy)
    self.cookies.set_persistent_storage(cookiesPath, storage)

Solution

  • All i had to do was connect to cookies changed signal.

    def cookies_change(self):
                print("Updating Cookies")
    
            self.cookies.connect("changed", cookies_change)