Search code examples
cookiespaypalwebviewwebkitpygtk

Delete cookies from python WebKit.WebView


I have a PyGtk app with an embedded WebKit.WebView. Users login to our service through the embedded browser and they can make payments in PayPal page.

This app is used by different users in the same session, so the problem is that PayPal bakes cookies and remembers the email of the last payment.

I just want to complete the payment and delete PayPal-related cookies from my WebView but I cannot find any documentation about removing cookies.

To re-create the webView is not an option because my web page is quite heavy to load.

Any help would be highly appreciated


Solution

  • I figured out by myself.

    First I added a custom cookiejar. This allows me to handle the cookies

    from gi.repository import Soup
    
    cookiejar = Soup.CookieJarText.new("cookies/biscottini.txt", False)
    cookiejar.set_accept_policy(Soup.CookieJarAcceptPolicy.ALWAYS)
    
    session = gi.repository.WebKit.get_default_session()
    session.add_feature(cookiejar)
    

    Then I found the Soup class structure and wrote down this simple snippet to clear the cookies (there's also a filter to delete only PayPal cookies)

    def clearCookies(deleteAll=False):       
    
        for cookie in cookiejar.all_cookies():
            if deleteAll or ('paypal' in cookie.get_domain().lower()):            
                cookiejar.delete_cookie(cookie)
    

    I now have a problem though: after clearing PayPal's cookies if I try to make a payment, I get the error: "Message Corrupt"