1

In my cookies folder, there are many files cookies1.txt, cookies2.txt, ... and below is my script, I try to use it to login facebook with cookies, with each cookie file it will load and execute an action then finish and perform the next load file, it works fine when I run each cookie one by one but when I put them all in a folder, it appears an error, can someone help me? Thank you.

import os
import pickle
import selenium.webdriver


driver = selenium.webdriver.Firefox()
files = os.listdir("cookies")
for f in files:
    cookies = pickle.load(open(f, "rb"))
    for cookie in cookies:
        driver.add_cookie(cookie)
        driver.get("http://www.facebook.com")

It shows:

Traceback (most recent call last):
  File "C:/Users/tuong cat/AppData/Local/Programs/Python/Python37-32/Lib/site-packages/new.py", line 10, in <module>
    driver.add_cookie(cookie)


  File "C:\Users\tuong cat\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 894, in add_cookie
    self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict})
  File "C:\Users\tuong cat\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)


  File "C:\Users\tuong cat\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidCookieDomainException: Message: Document is cookie-averse
Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
vantuong
  • 161
  • 1
  • 12

2 Answers2

2

I think you have to add the cookies after open the URL

driver.get("http://www.facebook.com")
for f in files:
    cookies = pickle.load(open(f, "rb"))
    for cookie in cookies:
        driver.add_cookie(cookie)
ewwink
  • 18,382
  • 2
  • 44
  • 54
1

1) Your files of cookies should be with ".pkl" extension.

2) Your files should be generated by pickle package after login action with your creds for Facebook under your particular domain.

3) You should load cookies files under your particular domain.

In your case, particular domain is facebook.com.

Hope it helps you!

PS: Here is my working example for Facebook: https://github.com/ratmirasanov/demo_test_automation_project/tree/master/login_with_using_cookies/.

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40