1

WARNING: cookies provided in this thread may already be invalid so you should do it with your own or some another cookies

I need to log in to my youtube account using cookies and selenium.

I can send a request to https://www.youtube.com/ with cookies and it will succeed:

to know if your request succeeds you should push ctrl+f and type topbarMenuButtonRenderer, it will be 2 results if you are logged in to the account and only 1 if you are not

login to youtube via postman

so the account is valid and you can log in to it with cookies, so now let's try to log in via selenium:

driver.get('https://www.youtube.com/')
driver.delete_all_cookies()

cookies_list = [{'domain': 'youtube.com', 'path': '/', 'name': 'YSC', 'value': 'a38PFuRxAE'},
                {'domain': 'youtube.com', 'path': '/', 'name': 'LOGIN_INFO',
                 'value': 'AFmmF2swRgIhAJrDBvelBtBPZeLLcvN5NdPjNC91wU4dwlXwLxGgLrwiAiEAiqfySzpzR-oQCHMjMwSrRTYX06SPyTTXVq7aknsa9m0:QUQ3MjNmd2xTMWtDckdmYUxWZ2wxLUhzemhua1VOanBqZGlMUzJoMnBVU1NHSnRwcE9EXzBfdnFLa1hhXzBWd3VGVmpiYktpVmVuTjhFd0NXUUN6ZVdMcUFXX281T1RjQTE2d2FaU0Y0OE1pT2pwbmtRTDM2VkhrM1RTM1F5ZjNCWEU1UWJSWnN0OVl2Nnh3WkV6NlRaMkQ5bkZ2NDlSZUh'},
                {'domain': 'youtube.com', 'path': '/', 'name': '__Secure-3PAPISID',
                 'value': 'Xu1s1vs4Mgl1xUEs/AVuFyeQXwPAFFeQY'},
                {'domain': 'youtube.com', 'path': '/', 'name': '__Secure-3PSID'},
                {'domain': 'youtube.com', 'path': '/', 'name': 'wide', 'value': ''},
                {'domain': 'youtube.com', 'path': '/', 'name': 'VISITOR_INFO1_LIVE', 'value': 'FI45r6mrQr'},
                {'domain': 'youtube.com', 'path': '/', 'name': 'PREF', 'value': 'tz=Africa.Casablanc'},
                {'domain': 'youtube.com', 'path': '/', 'name': '__Secure-3PSIDCC',
                 'value': 'AEf-XMSrvxDUtrK-9pfKiHiKp-G2KXstszEs3VKc8QNAOQne87VY1Gf5qRFmh30apZ0Y23NVXdx'}]

for cookie in cookies_list:
    driver.add_cookie(cookie)

print(driver.get_cookies())

and nothing works, I am not logged in youtube account, I just see this page: login fail

as you can see after I added cookies I printed driver.get_cookies(), and here is what it returned:

[{'domain': '.youtube.com', 'httpOnly': False, 'name': '__Secure-3PSIDCC', 'path': '/', 'secure': True,
  'value': 'AEf-XMSrvxDUtrK-9pfKiHiKp-G2KXstszEs3VKc8QNAOQne87VY1Gf5qRFmh30apZ0Y23NVXdx'},
 {'domain': '.youtube.com', 'httpOnly': False, 'name': 'PREF', 'path': '/', 'secure': True,
  'value': 'tz=Africa.Casablanc'},
 {'domain': '.youtube.com', 'httpOnly': False, 'name': 'VISITOR_INFO1_LIVE', 'path': '/', 'secure': True,
  'value': 'FI45r6mrQr'},
 {'domain': '.youtube.com', 'httpOnly': False, 'name': '__Secure-3PSID', 'path': '/', 'secure': True,
  'value': 'OwgduM77qro_1Obb2aOFY-SUCodOe-WbFGBjwBesoFqHgxebvyLaYFcsEyPQlLwd1Prm1w'},
 {'domain': '.youtube.com', 'httpOnly': False, 'name': 'wide', 'path': '/', 'secure': True, 'value': ''},
 {'domain': '.youtube.com', 'httpOnly': False, 'name': '__Secure-3PAPISID', 'path': '/', 'secure': True,
  'value': 'Xu1s1vs4Mgl1xUEs/AVuFyeQXwPAFFeQY'},
 {'domain': '.youtube.com', 'httpOnly': False, 'name': 'LOGIN_INFO', 'path': '/', 'secure': True,
  'value': 'AFmmF2swRgIhAJrDBvelBtBPZeLLcvN5NdPjNC91wU4dwlXwLxGgLrwiAiEAiqfySzpzR-oQCHMjMwSrRTYX06SPyTTXVq7aknsa9m0:QUQ3MjNmd2xTMWtDckdmYUxWZ2wxLUhzemhua1VOanBqZGlMUzJoMnBVU1NHSnRwcE9EXzBfdnFLa1hhXzBWd3VGVmpiYktpVmVuTjhFd0NXUUN6ZVdMcUFXX281T1RjQTE2d2FaU0Y0OE1pT2pwbmtRTDM2VkhrM1RTM1F5ZjNCWEU1UWJSWnN0OVl2Nnh3WkV6NlRaMkQ5bkZ2NDlSZUh'},
 {'domain': '.youtube.com', 'httpOnly': False, 'name': 'YSC', 'path': '/', 'secure': True, 'value': 'a38PFuRxAE'}]

they are exactly the same as the ones I used above however, if I add this code:

driver.refresh()
print(driver.get_cookies())

what I see is:

[{'domain': '.youtube.com', 'expiry': 1699659768, 'httpOnly': False, 'name': 'CONSENT', 'path': '/', 'secure': True,
  'value': 'PENDING+879'},
 {'domain': '.youtube.com', 'expiry': 1699227767, 'httpOnly': True, 'name': '__Secure-YEC', 'path': '/',
  'sameSite': 'Lax', 'secure': True, 'value': 'CgtFOHBmRnprNldvRSjyz_2ZBg%3D%3D'},
 {'domain': '.youtube.com', 'expiry': 1699659769, 'httpOnly': False, 'name': 'PREF', 'path': '/', 'secure': True,
  'value': 'tz=Europe.London&f4=4000000'},
 {'domain': '.youtube.com', 'httpOnly': False, 'name': '__Secure-3PSIDCC', 'path': '/', 'secure': True,
  'value': 'AEf-XMSrvxDUtrK-9pfKiHiKp-G2KXstszEs3VKc8QNAOQne87VY1Gf5qRFmh30apZ0Y23NVXdx'},
 {'domain': '.youtube.com', 'httpOnly': False, 'name': '__Secure-3PSID', 'path': '/', 'secure': True,
  'value': 'OwgduM77qro_1Obb2aOFY-SUCodOe-WbFGBjwBesoFqHgxebvyLaYFcsEyPQlLwd1Prm1w'},
 {'domain': '.youtube.com', 'httpOnly': False, 'name': 'wide', 'path': '/', 'secure': True, 'value': ''},
 {'domain': '.youtube.com', 'httpOnly': False, 'name': '__Secure-3PAPISID', 'path': '/', 'secure': True,
  'value': 'Xu1s1vs4Mgl1xUEs/AVuFyeQXwPAFFeQY'}]

and it is not the same as it was so as I understand youtube assigned me other cookies, but why youtube did it?

when I use the cookie quick manager extension (source code) in firefox everything works fine and I can log in youtube account

so how can I log into my youtube account with cookies using selenium and what is the reason I can not add cookies? I tried selenium-wire and default selenium and got the same problem

If I try to log in manually via selenium browser I see this:

What is the problem? I passed all tests on https://bot.sannysoft.com/ and selenium's result is even better than mine - https://i.imgur.com/1MfSu9L.mp4 enter image description here

1bura
  • 23
  • 6

2 Answers2

1

Try resort into using undetected_chromedriver: https://pypi.org/project/undetected-chromedriver/

lplpqq
  • 11
  • 2
0

Here is what you can do:

In order to use seleniumwire you have to add the certificate to your browser. https://github.com/wkeeling/selenium-wire#certificates

  1. Login with your email account.
from seleniumwire.undetected_chromedriver.v2 import Chrome, ChromeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
import time
import pickle


if __name__ == "__main__":
    options = {}
    chrome_options = ChromeOptions()
    chrome_options.add_argument('--user-data-dir=hash')
    chrome_options.add_argument("--disable-gpu")
    chrome_options.add_argument("--incognito")
    chrome_options.add_argument("--disable-dev-shm-usage")
    # chrome_options.add_argument("--headless")
    browser = Chrome(seleniumwire_options=options, options=chrome_options)
    wait = WebDriverWait(browser, 10)
    browser.get('https://gmail.com')
    wait.until(ec.presence_of_element_located((By.XPATH, '//*[@id="identifierId"]'))).send_keys('EMAIL')
    wait.until(ec.presence_of_element_located((By.XPATH, '//*[@id="identifierNext"]/div/button'))).click()
    time.sleep(5)
    wait.until(ec.presence_of_element_located((By.XPATH, '//*[@id="password"]/div[1]/div/div[1]/input'))).send_keys('PASSWORD')
    wait.until(ec.presence_of_element_located((By.XPATH, '//*[@id="passwordNext"]/div/button'))).click()

    time.sleep(50)
    browser.get('https://www.youtube.com')
    pickle.dump(browser.get_cookies(), open('test_cookies.pkl', 'wb'))
    print('Dumped Successfully')

    time.sleep(10)
    browser.close()
  1. Now you have cookies stored, use them.
from seleniumwire.undetected_chromedriver.v2 import Chrome, ChromeOptions
import time
import pickle
if __name__ == "__main__":
    options = {}
    chrome_options = ChromeOptions()
    chrome_options.add_argument('--user-data-dir=hash')
    chrome_options.add_argument("--disable-gpu")
    chrome_options.add_argument("--incognito")
    chrome_options.add_argument("--disable-dev-shm-usage")
    # chrome_options.add_argument("--headless")
    browser = Chrome(seleniumwire_options=options, options=chrome_options)
    browser.get('https://www.youtube.com')
    time.sleep(10)
    with open('test_cookies.pkl', 'rb') as f:
        cookies = pickle.loads(f.read())
        print(cookies)

    for cookie in cookies:
        browser.add_cookie({'name': cookie['name'], 'value': cookie['value']})

    browser.refresh()
    time.sleep(50)
    browser.close()

https://i.stack.imgur.com/dbTn9.jpg

Anandesh Sharma
  • 436
  • 6
  • 22