Skip to content

Instantly share code, notes, and snippets.

@3dprogramin
Last active January 5, 2024 19:51
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save 3dprogramin/708a792b64482a8cb7d613beeab8c0e8 to your computer and use it in GitHub Desktop.
Save 3dprogramin/708a792b64482a8cb7d613beeab8c0e8 to your computer and use it in GitHub Desktop.
Import cookies from text file into selenium webdriver
from selenium import webdriver
from time import sleep
# read cookies from file
# format
# ... expiry1 key1 value1
# ... expiry2 key2 value2
def read_cookies(p = 'cookies.txt'):
cookies = []
with open(p, 'r') as f:
for e in f:
e = e.strip()
if e.startswith('#'): continue
k = e.split('\t')
if len(k) < 3: continue # not enough data
# with expiry
cookies.append({'name': k[-2], 'value': k[-1], 'expiry': int(k[-3]})
return cookies
# import cookies into selenium webdriver (to authenticate)
def run():
cookies = read_cookies()
print ('[+] Read {} cookies'.format(len(cookies)))
print (cookies)
d = webdriver.Chrome()
d.get('https://facebook.com')
sleep(2)
print(d.get_cookies())
# adding cookies
for c in cookies: d.add_cookie(c)
print ('')
print ('[+] Added cookies')
print(d.get_cookies())
d.get('https://facebook.com')
sleep(50)
def main():
print ('[+] Selenium cookies importer')
try: run()
except Exception as ex: print ('[!] Error: {}'.format(ex))
finally: print ('[+] Finished !')
main()
@3a1
Copy link

3a1 commented Apr 19, 2023

thx you very mch bro, i dont even know how much more time i would have spent on this and in general where i would be right now if i hadnt seen your code, thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment