I am trying to loged in using cookies and my main goal is to skip log in page via selenium webdriver add_cookie method. I've created csv file with cookies. In csv I have 3 columns which are Name, Value, Domain and all cookies were already added in this file. But while running my code I see that webdriver can't bypass login step and I am getting the below exception from selenium library.
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: missing 'cookie'
My code is below:
from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import pandas as pd
options = Options()
options.binary_location = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"
link = "https://**********"
browser = webdriver.Chrome(options=options,
service=Service(ChromeDriverManager().install()))
browser.implicitly_wait(5)
browser.get(link)
def get_cookies_values(file):
dict_reader = pd.read_csv(file, encoding='unicode_escape')
list_of_dicts = list(dict_reader)
return list_of_dicts
cookies = get_cookies_values('cookies.csv')
for i in cookies:
browser.add_cookie(i)
time.sleep(5)
browser.quit()
Update
I've changed code and replaced encoding='unicode_escape' with encoding='utf-8'. The reason of such changes is the fact that I found that if I am using encoding='unicode_escape', then nothing read from my file. I found it using print function. Due to this I've replaced the econding option to 'utf-8' and moreover I've tried with 'utf-8-sig' but now I am getting the another error which is below:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x85 in position 2391: invalid start byte
So, for some reason I can't read my csv file. Could someone clarify for me why? And how can I provide cookies to my driver.
CodePudding user response:
Instead of storing the cookies within a csv file you can use pickle to store the cookies once the user had logged into the website and retrive them later as follows:
Storing the cookies:
import pickle
driver.get(home_page_url)
# steps to login
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))
Adding back the cookies:
driver.get(home_page_url)
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
driver.add_cookie(cookie)
driver.get(user_authenticated_page)
References
You can find a couple of relevanr detailed discussion in: