Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python selenium get cookie and store cookie

from selenium import webdriver
import json
 
browser = webdriver.Chrome()
url = 'https://www.google.com/'
browser.get(url)
cookie = browser.get_cookies()
browser.quit()
 
with open('cookie', 'w') as j_out:
    json.dump(cookie, j_out)
with open('cookie') as j:
    saved_cookie = json.load(j)
 
 
#print(saved_cookie) # The whole cookie as a list with dictionaries 
print(saved_cookie[0]['domain'])
print(saved_cookie[0]['httpOnly'])
Comment

python selenium save cookies

# read cookies

import pickle
import selenium.webdriver 

driver = selenium.webdriver.Firefox()
driver.get("http://www.google.com")
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
    driver.add_cookie(cookie)
Comment

python selenium save cookies

# save cookies 

import pickle
import selenium.webdriver 

driver = selenium.webdriver.Firefox()
driver.get("http://www.google.com")
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))
Comment

save cookies and load cookies using python-selenium-webdriver

$ cat work-auth.py
#!/usr/bin/python3

# Setup:
# sudo apt-get install chromium-chromedriver
# sudo -H python3 -m pip install selenium

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--user-data-dir=chrome-data")
driver = webdriver.Chrome('/usr/bin/chromedriver',options=chrome_options)
chrome_options.add_argument("user-data-dir=chrome-data")
driver.get('https://www.somedomainthatrequireslogin.com')
time.sleep(30)  # Time to enter credentials
driver.quit()

$ cat work.py
#!/usr/bin/python3

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--user-data-dir=chrome-data")
driver = webdriver.Chrome('/usr/bin/chromedriver',options=chrome_options)
driver.get('https://www.somedomainthatrequireslogin.com')  # Already authenticated
time.sleep(10)
driver.quit()
Comment

selenium pickle get cookies python save cookies

import pickle
import os
from selenium import webdriver
import time


option = webdriver.ChromeOptions()
option.add_argument("--no-sandbox")        
driver = webdriver.Chrome(options=option)
driver.get("https://google.com")
time.sleep(5)
if os.path.exists('cookies.pkl'):
    cookies = pickle.load(open("cookies.pkl", "rb"))
    for cookie in cookies:
        driver.add_cookie(cookie)
    driver.refresh()
    sleep(5)
pickle.dump(driver.get_cookies(), open("cookies.pkl", "wb"))
Comment

PREVIOUS NEXT
Code Example
Python :: how to know where python is installed on windows 
Python :: Python - Drop row if two columns are NaN 
Python :: django models distinct 
Python :: median in python 
Python :: all possible combinations of parameters 
Python :: remove n from string python 
Python :: how to roll longitude coordinate 
Python :: openpyxl delete column by name 
Python :: python join two lists as dictionary 
Python :: dataframe delete row 
Python :: how to log ip addresses in python 
Python :: pandas groupby histogram 
Python :: python finite difference approximation backward difference 
Python :: Installing python module from within code 
Python :: python multiply one column of array by a value 
Python :: connecting google colab to local runtime 
Python :: how to change icon in pygame 
Python :: how to remove all zeros from a list in python 
Python :: read csv exclude index pandas 
Python :: print python 
Python :: python print do not use scientific notation 
Python :: python dataclass default factory 
Python :: how to check if index is out of range python 
Python :: print variable in string python 
Python :: plot distribution seaborn 
Python :: pyplot bar plot colur each bar custom 
Python :: full screen jupyter notebook 
Python :: remove outliers in dataframe 
Python :: flask render error template 
Python :: python delete duplicate lines in file 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =