Add cookies in selenium to automate your work
This blog is about accessing the account with cookies while working with the selenium web driver
pre-requistics
Python
Selenium
get started:
Import web driver from selenium module and create an object of chrome web driver and give chrome driver local path.
from selenium import webdriver
driver = webdriver.Chrome(r"Path of chrome driver")
This will open a chrome driver, now request a URL by the get method
driver.get("https://leetcode.com/problems/two-sum/")
Save your cookies in the cookies.json file in JSON format. Import JSON and open the file as f.
Add cookies to the chrome web driver, and while adding it you may counter the error with sameSite access disabled for that change the 'None' to 'Strict'.
import json
with open('cookies.json', 'r') as f:
cookies = json.load(f) #stoting cookies
for cookie in cookies:
#set the sameSite attribute to 'Strict' to avoid the error
if 'sameSite' in cookie:
cookie['sameSite'] = 'Strict'
driver.add_cookie(cookie) #add the cookies
time.sleep(10) # add wait to load the cookies
Now you can access the site with the given cookies by visiting the URL
driver.get("https://leetcode.com/problems/two-sum/")
print(driver.get_cookies())
I hope this blog helps you to understand how to add and work with cookies in selenium. ๐ฆ
connect with me ๐จโ๐ป Gaurav Kushwaha
ย