how to pass selenium browser cookie

  cookies, python, selenium

Im grabbing a cookie with a driver storing it on my electron app as a temp string and grabbing it when the bot starts only issue is I’m not able to pass the cookie with driver.add_cookie() because its a string does anyone know how I can deal with this?

my current method is this

box_of_cookies = "{'domain': '.kith.com', 'expiry': 1662256296, 'httpOnly': False, 'name': 'geolizr_geodata', 'path': '/', 'sameSite': 'Strict', 'secure': True, 'value': '{%22as%22:%22AS701%20MCI%20Communications%20Services%2C%20Inc.%20d/b/a%20Verizon%20Business%22%2C%22asname%22:%22UUNET%22%2C%22mobile%22:false%2C%22proxy%22:false%2C%22city%22:%22East%20Orange%22%2C%22currency%22:{%22code%22:%22USD%22}%2C%22country%22:{%22code%22:%22US%22%2C%22country%22:%22United%20States%22}%2C%22countryCode%22:%22US%22%2C%22continent%22:%22North%20America%22%2C%22continentCode%22:%22NA%22%2C%22isp%22:%22MCI%20Communications%20Services%2C%20Inc.%20d/b/a%20Verizon%20Business%22%2C%22lat%22:40.7523%2C%22lon%22:-74.2172%2C%22org%22:%22Verizon%20Communications%22%2C%22query%22:%2274.102.170.52%22%2C%22region%22:%22NJ%22%2C%22regionName%22:%22New%20Jersey%22%2C%22status%22:%22success%22%2C%22timezone%22:%22America/New_York%22%2C%22zip%22:%2207018%22%2C%22cloudflare%22:%22US%22%2C%22ttl%22:1129%2C%22env%22:%22PROD%22%2C%22version%22:%22ip-api%20cached%22%2C%22currencyCode%22:%22USD%22%2C%22countryName%22:%22United%20States%22%2C%22service%22:%22ip.lovely-app.com%22}'}, {'domain': '.kith.com', 'expiry': 1630722096, 'httpOnly': False, 'name': '_shopify_sa_t', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '2021-09-04T01%3A51%3A36.257Z'}, {'domain': 'kith.com', 'expiry': 1693792296, 'httpOnly': False, 'name': '__kla_id', 'path': '/', 'secure': False, 'value': 'eyIkcmVmZXJyZXIiOnsidHMiOjE2MzA3MjAyOTMsInZhbHVlIjoiIiwiZmlyc3RfcGFnZSI6Imh0dHBzOi8va2l0aC5jb20vYWNjb3VudC9sb2dpbiJ9LCIkbGFzdF9yZWZlcnJlciI6eyJ0cyI6MTYzMDcyMDI5NiwidmFsdWUiOiIiLCJmaXJzdF9wYWdlIjoiaHR0cHM6Ly9raXRoLmNvbS9hY2NvdW50L2xvZ2luIn19'}, {'domain': 'kith.com', 'expiry': 1662256295, 'httpOnly': True, 'name': 'secure_customer_sig', 'path': '/', 'secure': True, 'value': 'ab5c745f4c26465aaac384544b1f5648'}, {'domain': '.kith.com', 'expiry': 1630722096, 'httpOnly': False, 'name': '_shopify_sa_p', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': ''}, {'domain': 'kith.com', 'expiry': 1631929894, 'httpOnly': True, 'name': 'cart_sig', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': '90d14af23f488d1426f499a12b8cfd28'}, {'domain': 'kith.com', 'expiry': 1662256296, 'httpOnly': False, 'name': 'KL_FORMS_MODAL', 'path': '/', 'secure': False, 'value': '{%22disabledForms%22:{}%2C%22viewedForms%22:{%22TFUkCm%22:3408578}}'}, {'domain': '.kith.com', 'expiry': 1630720353, 'httpOnly': False, 'name': '_gat_gtag_UA_62344036_3', 'path': '/', 'secure': False, 'value': '1'}, {'domain': 'kith.com', 'expiry': 1630806695, 'httpOnly': True, 'name': '_secure_session_id', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': 'ca70a6a5d3d396c11cc9a3513cb7cfb4'}, {'domain': '.kith.com', 'expiry': 1630722096, 'httpOnly': False, 'name': '_shopify_s', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '509e92eb-30fd-4db1-b8f4-be1c24db64bd'}, {'domain': 'kith.com', 'expiry': 1630723892, 'httpOnly': False, 'name': 'shopify_pay_redirect', 'path': '/', 'secure': False, 'value': 'pending'}, {'domain': '.kith.com', 'expiry': 1662256295, 'httpOnly': False, 'name': '_y', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '5bcc7bb2-1969-4a02-85bf-3d4a91fe7b1b'}, {'domain': '.kith.com', 'httpOnly': False, 'name': 'GlobalE_Full_Redirect', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'false'}, {'domain': '.kith.com', 'expiry': 1693792296, 'httpOnly': False, 'name': '_ga', 'path': '/', 'secure': False, 'value': 'GA1.2.465106694.1630720293'}, {'domain': '.kith.com', 'expiry': 1630806696, 'httpOnly': False, 'name': '_gid', 'path': '/', 'secure': False, 'value': 'GA1.2.703132267.1630720293'}, {'domain': 'kith.com', 'expiry': 1630722095, 'httpOnly': False, 'name': 'dynamic_checkout_shown_on_cart', 'path': '/', 'secure': False, 'value': '1'}, {'domain': '.kith.com', 'expiry': 1662256295, 'httpOnly': False, 'name': '_shopify_y', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '5bcc7bb2-1969-4a02-85bf-3d4a91fe7b1b'}, {'domain': 'kith.com', 'expiry': 1630722091, 'httpOnly': False, 'name': '_shopify_country', 'path': '/', 'secure': False, 'value': 'United+States'}, {'domain': '.kith.com', 'expiry': 1631929891, 'httpOnly': True, 'name': '_landing_page', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '%2Faccount%2Flogin'}, {'domain': 'kith.com', 'httpOnly': False, 'name': 'snize-recommendation', 'path': '/', 'secure': False, 'value': 'syvcewxzszd'}, {'domain': '.kith.com', 'expiry': 1693792293, 'httpOnly': False, 'name': 'GlobalE_CT_Data', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '%7B%22CUID%22%3A%22210526597.612443469.583%22%2C%22CHKCUID%22%3Anull%7D'}, {'domain': '.kith.com', 'expiry': 1630722096, 'httpOnly': False, 'name': '_s', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '509e92eb-30fd-4db1-b8f4-be1c24db64bd'}, {'domain': '.kith.com', 'expiry': 1631929891, 'httpOnly': True, 'name': '_orig_referrer', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': ''}, {'domain': '.kith.com', 'expiry': 1662256297, 'httpOnly': False, 'name': '__zlcmid', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '15ukIyQ66OqABfm'}, {'domain': '.kith.com', 'httpOnly': False, 'name': 'GlobalE_SupportThirdPartCookies', 'path': '/', 'secure': False, 'value': 'true'}, {'domain': '.kith.com', 'expiry': 1630979493, 'httpOnly': False, 'name': 'GlobalE_Data', 'path': '/', 'secure': False, 'value': '%7B%22countryISO%22%3A%22US%22%2C%22currencyCode%22%3A%22USD%22%2C%22cultureCode%22%3A%22en-US%22%7D'}, {'domain': '.kith.com', 'expiry': 1630720353, 'httpOnly': False, 'name': '_gat', 'path': '/', 'secure': False, 'value': '1'}, {'domain': 'kith.com', 'expiry': 1631929895, 'httpOnly': False, 'name': 'cart_currency', 'path': '/', 'secure': False, 'value': 'USD'}"



#adds cookie to driver
def parse_Cookie_string(driver,box_of_cookies):
   #print(cookie.split("},"))
   cookies_list = list(box_of_cookies.split("},"))
   for cookie in cookies_list:
       #check if its the last number element in the list if it is dont add the }
       if(cookie == cookies_list[-1]):
         pass
      else:
         cookie += "}" 
         print(cookie)
         driver.add_cookie(json.loads(cookie)

but I just get a error saying -> Expecting value: line 1 column 59 (char 58)

stack trace

   Expecting value: line 1 column 59 (char 58)

  During handling of the above exception, another exception occurred:

   File    "/Users/raye/PycharmProjects/sneakerBots/kithbot.py", line 64, in parse_Cookie_string
driver.add_cookie(json.loads(cookie))
   File "/Users/raye/PycharmProjects/sneakerBots/kithbot.py", line 213, in K_Step_Four
parse_Cookie_string(driver,MY_COOKIE)                   #|
   File "/Users/raye/PycharmProjects/sneakerBots/kithbot.py", line 412, in START_KithBot
   K_Step_Four(driver,email,fname,lname,address,city,zipcode,phoneNumber,account_email,account_password,COOKIE)

Source: Python Questions

LEAVE A COMMENT