Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python selenium, retrieving value of cookie and inserting into a GET

Tags:

python

I wanted to create a script that automatically downloads a tar file from a site. The problem is, I need to create an HTTP GET that contains the value of a JSESSIONID cookie in it in order for the file to download. The application is flash or otherwise I would retrieve the file in a more normal fashion. When I print all_cookies, I get the output below which is before my script. I want the value of the JSESSIONID '8430..' etc. Any insight as to how I can do this would be greatly appreciated..

[{u'domain': u'my.site.com', u'name': u'JSESSIONID', u'value': u'8430c050201161 b5404d52194a5445561a02', etc.. ]

And here is my script:

from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys
import time
import datetime
import os
import shutil

dt = str(datetime.datetime.now().strftime("%m-%d-%Y"))


fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir", os.getcwd())
fp.update_preferences()
driver = webdriver.Firefox(fp)
wait = ui.WebDriverWait(driver,10)

driver.get("https://my.site.com")
un = driver.find_element_by_name("email")
pw = driver.find_element_by_name("password")
un.send_keys('[email protected]')
pw.send_keys("password")
driver.find_element_by_name("login").click()

all_cookies = driver.get_cookies()
print all_cookies
like image 796
AgileDan Avatar asked Mar 23 '26 02:03

AgileDan


1 Answers

get_cookies() returns a set of dictionaries, corresponding to cookies.

from your example data, you can retrieve individual name/value pairs by doing a dictionary lookup by "name" and "value" keys.

for example:

all_cookies[0]['name']  # returns 'JSESSIONID'
all_cookies[0]['value']  # returns '8430c050201161b5404d52194a5445561a02'
like image 143
Corey Goldberg Avatar answered Mar 25 '26 15:03

Corey Goldberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!