简体   繁体   中英

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: missing 'cookie' while adding cookies using selenium webdriver

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.

Instead of storing the within a csv file you can use 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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question selenium.common.exceptions.InvalidArgumentException: Message: invalid argument error invoking get() using Selenium Webdriver through Python selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: missing 'ELEMENT' error when switching frame in Selenium selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: File not found error while uploading file using Selenium Python selenium.common.exceptions.InvalidArgumentException: Message: invalid argument while iterating through a list of urls and passing as argument to get() selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'using' must be a string using waits and expected conditions Python selenium for write review google maps (selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator ) selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator error using find_element('username') Selenium Python URL must be a string selenium - selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'url' must be a string Selenium By.CSS_SELECTOR selenium.common.exceptions.InvalidArgumentException: Message: invalid argument selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: File not found error invoking send_keys() using Selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM