L
L
LordGod2021-12-03 22:12:04
Python
LordGod, 2021-12-03 22:12:04

How to convert cookies from selenium to work with requests?

I want to log in to the site using cookies.
I am using requests. I take cookies from selenium.
How do I convert the cookies that I took from selenium to a format that requests understands.
Or how to get cookies without selenium?
Initially, it is not possible to log in to the site through requests.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ross Alex, 2021-12-04
@Wacdis

Example, Selenium Grid 4, Node-chrome (in containers)

import os, json, requests
from selenium import webdriver
from selenium.common import exceptions
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from webdriver_manager.chrome import ChromeDriverManager

if __name__ == '__main__':
  options = webdriver.ChromeOptions()
  options.add_argument('user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36')
  options.add_argument('--headless')
  options.add_argument('--user-data-dir=/app/google-chrome')
  
  # В моем случае, это Selenium Grid 4
  driver = webdriver.Remote(command_executor=grid_url, options=options, desired_capabilities={})
  driver.get("https://domain.com/some-page-with-cookies")
  
  # Делаем авторизацию, если нужна
  # ...
  
  # Забираем куки и удаляем сессию
  driver_cookies = driver.get_cookies()
  driver.close()
  driver.quit()
  cookies = {}
  for cookie in driver_cookies:
    cookies[ cookie['name'] ] = cookie['value']
  r = requests.get('https://domain.com/target-url', cookies=cookies)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question