0

I'm using selenium 3.141.0 with chromedriver 86.0.4240.22 to log into a website, store the cookies. During the next session I wish to use the existing cookies to prevent doing the log in again. However, it does not work. Everything seems to run fine, but the site https://primexbt.com does not ack the existing cookies. What can I do?

    options = Options()
    options.add_argument('--dns-prefetch-disable')
    options.add_argument('--no-sandbox')
    options.add_argument('--lang=en-US')
    options.add_argument('--disable-setuid-sandbox')
    chrome_prefs = {
        'intl.accept_languages': 'en-US',
    }
    options.add_experimental_option('prefs', chrome_prefs)
    self.driver = webdriver.Chrome(options=options)

    # load cookies
    cookies = get_cookies()
    if cookies:
        self.driver.get(self.URL_PRIMEXBT)
        for cookie in cookies:


            self.driver.add_cookie(cookie)
Tjorriemorrie
  • 16,818
  • 20
  • 89
  • 131

1 Answers1

1

By default - a new ChromeDriver session creates a new profile data dir - so you don't have cookies from your previous session. I would suggest to either:

  1. Start WebDriver with a profile data that contains your authorization cookie
options.add_argument('user-data-dir')
  1. Do the authorization by direct http call/API call, bypassing WebDriver (to speed up the test) - and pass the response cookies to WebDriver
  2. Or store authorization cookies and load them - e.g. using this solution: How to save and load cookies using Python + Selenium WebDriver
Piotr M.
  • 385
  • 2
  • 8
  • As I've stated I am saving and loading the cookies correctly, I can see that during debugging. – Tjorriemorrie Oct 21 '20 at 07:45
  • Could you share get_cookies implementation? – Piotr M. Oct 21 '20 at 07:53
  • And could you check if cookie properties of properly set cookie are the same that properties of cookie added by add_cookie selenium method (especially - Domain)? – Piotr M. Oct 21 '20 at 08:00
  • hi, it works if I use `options.add_argument('user-data-dir`. If you can replace the bullets with that then I'll accept. Using profile I don't even need to re-add the cookies as it is included. Got the idea from your 1st sentence, thanks :) – Tjorriemorrie Oct 21 '20 at 10:27
  • hi - I've added this solution with starting WebDriver with a profile data – Piotr M. Oct 21 '20 at 10:52