Python Urllib2 Cookiejar with Selenium

I am using python urllib2 and cookiejar to access the website. The last page of the site is too complicated to work with urllib2 (it uses javascript and frames), so I would like to open it with Selenium, but I need to transfer cookies to selenium before I can continue.

I have my cookiejar setting as follows

cj = cookielib.CookieJar() 

Is there any way to iterate over this and display each cookie? It looks like I can set cookies in selenium using:

 Cookie cookie = new Cookie("key", "value"); driver.manage().addCookie(cookie); 
+6
source share
1 answer

In fact, you can just iterate over the cj object. Here is a simple example after opening reddit:

 In [40]: for c in cj: ....: print c.name, c.value ....: ....: reddit_first %7B%22firsttime%22%3A%20%22first%22%7D 

I have not previously passed them to Selenium in this method, but I assume that you can use the structure described above.

+2
source

All Articles