使用Selenium的Python Urllib2 Cookiejar

xur*_*r17 6 python cookies selenium urllib2

我使用python urllib2和cookiejar来访问网站.该网站的最后一页太复杂,无法处理urllib2(它使用javascript和框架),所以我想用Selenium打开它,但我需要将我的cookie转移到selenium才能继续.

我的cookiejar设置如下

cj = cookielib.CookieJar()
Run Code Online (Sandbox Code Playgroud)

有没有办法迭代这个,并输出每个cookie?看起来我可以使用以下方法在selenium中设置cookie:

Cookie cookie = new Cookie("key", "value");
driver.manage().addCookie(cookie);
Run Code Online (Sandbox Code Playgroud)

Roc*_*key 2

实际上,您可以迭代该cj对象。这是打开 reddit 后的一个简单示例:

In [40]: for c in cj:
   ....:     print c.name, c.value
   ....:     
   ....:     
reddit_first %7B%22firsttime%22%3A%20%22first%22%7D
Run Code Online (Sandbox Code Playgroud)

我之前没有用该方法将它们转移到 Selenium,但我假设您可以使用上面概述的结构。