Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Urllib2 Cookiejar with Selenium

I am using python urllib2 and a cookiejar to access a website. The last page of the site is too complex to handle with urllib2 (it uses javascript and frames), so I'd like to open it using Selenium, but I need to transfer my cookies over to selenium before I can proceed.

I have my cookiejar setup as follows

cj = cookielib.CookieJar()

Is there some way to iterate over this, and output each cookie? It looks like I can set the cookies in selenium using:

Cookie cookie = new Cookie("key", "value");
driver.manage().addCookie(cookie);
like image 530
xur17 Avatar asked Dec 19 '12 19:12

xur17


1 Answers

You can actually 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 haven't transferred them to Selenium in that method before, but I assume you can use the structure you have outlined above.

like image 168
RocketDonkey Avatar answered Oct 12 '22 23:10

RocketDonkey