Python Chrome驱动程序无法获取cookie?

Python Chrome驱动程序无法获取cookie?,python,Python,我正在使用selenium和chrome驱动程序,试图从google my code获取一些cookie: import json from selenium import webdriver import time taskID = input("Choose task: ") driver = webdriver.Chrome("chromedriver.exe") driver.get("https://accounts.google.com/ServiceLogin?service=

我正在使用selenium和chrome驱动程序,试图从google my code获取一些cookie:

import json
from selenium import webdriver
import time

taskID = input("Choose task: ")

driver = webdriver.Chrome("chromedriver.exe")
driver.get("https://accounts.google.com/ServiceLogin?service=accountsettings&passive=1209600&osid=1&continue=https://myaccount.google.com/intro&followup=https://myaccount.google.com/intro")
while "sign" in driver.current_url:
    continue
driver.quit()
gcooks = driver.get_cookies()
with open('cookies'+str(taskID)+'.json', 'w') as outfile:
    json.dump(gcooks, outfile,indent=4)
但是,它返回此错误:

Traceback (most recent call last):
  File "cookieinjector.py", line 12, in <module>
    gcooks = driver.get_cookies()
  File "C:\Users\ninja_000\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 825, in get_cookies
    return self.execute(Command.GET_ALL_COOKIES)['value']
  File "C:\Users\ninja_000\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 310, in execute
    response = self.command_executor.execute(driver_command, params)
  File "C:\Users\ninja_000\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 466, in execute
    return self._request(command_info[0], url, body=data)
  File "C:\Users\ninja_000\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 490, in _request
    resp = self._conn.getresponse()
  File "C:\Users\ninja_000\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1331, in getresponse
    response.begin()
  File "C:\Users\ninja_000\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "C:\Users\ninja_000\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:\Users\ninja_000\AppData\Local\Programs\Python\Python36\lib\socket.py", line 586, in readinto
    return self._sock.recv_into(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
回溯(最近一次呼叫最后一次):
文件“cookieinjector.py”,第12行,在
gcooks=driver.get_cookies()
文件“C:\Users\ninja\u 000\AppData\Local\Programs\Python36\lib\site packages\selenium\webdriver\remote\webdriver.py”,第825行,在get\u cookies中
返回self.execute(Command.GET_ALL_COOKIES)['value']
文件“C:\Users\ninja\u 000\AppData\Local\Programs\Python36\lib\site packages\selenium\webdriver\remote\webdriver.py”,第310行,在execute中
响应=self.command\u executor.execute(driver\u command,params)
文件“C:\Users\ninja\u 000\AppData\Local\Programs\Python36\lib\site packages\selenium\webdriver\remote\remote\u connection.py”,第466行,执行
返回self.\u请求(命令信息[0],url,正文=数据)
文件“C:\Users\ninja\u 000\AppData\Local\Programs\Python36\lib\site packages\selenium\webdriver\remote\remote\u connection.py”,第490行,在请求中
resp=self.\u conn.getresponse()
文件“C:\Users\ninja\u 000\AppData\Local\Programs\Python\Python36\lib\http\client.py”,第1331行,在getresponse中
response.begin()
文件“C:\Users\ninja\u 000\AppData\Local\Programs\Python\Python36\lib\http\client.py”,第297行,在begin中
版本、状态、原因=self.\u读取\u状态()
文件“C:\Users\ninja\u 000\AppData\Local\Programs\Python\Python36\lib\http\client.py”,第258行,处于读取状态
line=str(self.fp.readline(_MAXLINE+1),“iso-8859-1”)
readinto中的文件“C:\Users\ninja\u 000\AppData\Local\Programs\Python\Python36\lib\socket.py”,第586行
返回自我。将袜子重新放入(b)
ConnectionAbortedError:[WinError 10053]主机中的软件中止了已建立的连接

非常感谢您的帮助

与我使用的版本的chromedriver被破坏有关,Firefox工作得更好

一些cookie无法检索Selenium。我假设这是HTTPonly cookies。您可以通过sqlite3从“/Default/cookies”文件中检索和解密cookies来解决此问题。到目前为止,我只找到了一种在Linux和Windows上实现这一点的方法。它与Google Chrome和Chrome一起工作

import sqlite3
import sys
from os import getenv, path
import os
from Crypto.Cipher import AES
from Crypto.Protocol.KDF import PBKDF2
import keyring

def get_cookies(url, cookiesfile):

    def chrome_decrypt(encrypted_value, key=None):
        dec = AES.new(key, AES.MODE_CBC, IV=iv).decrypt(encrypted_value[3:])
        decrypted = dec[:-dec[-1]].decode('utf8')
        return decrypted

    cookies = []
    if sys.platform == 'win32':
        import win32crypt
        conn = sqlite3.connect(cookiesfile)
        cursor = conn.cursor()
        cursor.execute(
            'SELECT name, value, encrypted_value FROM cookies WHERE host_key == "' + url + '"')
        for name, value, encrypted_value in cursor.fetchall():
            if value or (encrypted_value[:3] == b'v10'):
                cookies.append((name, value))
            else:
                decrypted_value = win32crypt.CryptUnprotectData(
                    encrypted_value, None, None, None, 0)[1].decode('utf-8') or 'ERROR'
                cookies.append((name, decrypted_value))

    elif sys.platform == 'linux':
        my_pass = 'peanuts'.encode('utf8')
        iterations = 1
        key = PBKDF2(my_pass, salt, length, iterations)
        conn = sqlite3.connect(cookiesfile)
        cursor = conn.cursor()
        cursor.execute(
            'SELECT name, value, encrypted_value FROM cookies WHERE host_key == "' + url + '"')
        for name, value, encrypted_value in cursor.fetchall():
            decrypted_tuple = (name, chrome_decrypt(encrypted_value, key=key))
            cookies.append(decrypted_tuple)
    else:
        print('This tool is only supported by linux and Mac')

    conn.close()
    return cookies


if __name__ == '__main__':
    pass
else:
    salt = b'saltysalt'
    iv = b' ' * 16
    length = 16

#get_cookies('YOUR URL FROM THE COOKIES', 'YOUR PATH TO THE "/Default/Cookies" DATA')