First introduction to Python series

Summary of Python selenium operation (3)

——Use locally stored cookies to automatically log in to any website


##1, Train of thought There are three ways to automatically log in to the website:

Compared with human-computer recognition, this is more challenging. If you are interested, you can try it

Directly log in cookies by reading the browser with local records

Log in using any browser by reading locally stored cookies

The second and third items are similar. They are all logged in by cookies, but the second item is that the local browser must be started to log in normally, while the third item can log in directly through the stored cookies in the subsequent process. Then my idea is: read and store cookies from the local browser -- > form a json file storing cookies -- > Add cookies to the browser by reading the json file to realize the login operation.



2, Code

The theory is established, and the code is as follows:

from selenium import webdriver
import json 
from selenium.webdriver.chrome.options import Options

#Loading cookie values with json files
def get_cookies_local(filename, driver):
    #Start chrome in the chrome installation location Exe file. The default installation location of chrome file is C:\Program Files\Google\Chrome\Application
    #If you run it for the first time, you need to run the command to open chrome in cmd: D: \ application \ Google \ chrome exe --remote-debugging-port=12306   
    print('\n\n\n\n  PLEASE PUT "D:\Application\Google\chrome.exe --remote-debugging-port=12306"\
    IN YOUR CMD \n\n\n')
    print("WAITTING FOR YOUR OPERATOR_____") 
    options = Options()
    options.add_experimental_option("debuggerAddress", "localhost:12306")
    setter = webdriver.Chrome(options=options)
    #Read cookies of web address on local browser
    setter.get("https://www.bilibili.com/")
    cookies= setter.get_cookies()
    with open(filename, 'w') as f:
        json.dump(cookies, f)
    setter.close()
    #Import cookie value directly after obtaining
    use_option(filename, driver)


use json The value of the text
def use_option(filename, driver):
    #Determine the return value by judging whether there is a corresponding cookie file or whether the cookie file has a value
    try:
        with open(filename, 'r') as f:
            arr = json.load(f)
            #If the cookie file is not empty, read and add cookies to the browser
            if arr != None:
                for i in range(len(arr)):
                    driver.add_cookie(arr[i])
                return True
            else:
                return False
    except Exception as e:
        return False
​
if __name__ == '__main__':
    driver = webdriver.Chrome()
    driver.get("https://www.bilibili.com/")
    #Use by judgment_ The return value of the option function determines whether to rewrite the json file
    if not use_option('cookies.json', driver):
        get_cookies_local('cookies.json', driver)
    #Sign in
    driver.get("https://www.bilibili.com/")



3, Run

You can see that there are no corresponding stored cookies JSON file, and then run the code directly:

Operation: after clicking run, the browser without local record will pop up directly. Open station b, and then manually open the browser with local record (for extracting and recording cookies) through the operation prompt. After the program closes the local browser, one more cookie will be found JSON file, and automatically log the unrecorded local browser into the corresponding account. Run the second time because cookies already exist JSON file, through which you can log in directly without opening the local browser again.



4, Summary

It is somewhat opportunistic to log in to the browser automatically through cookies, but it is also a solution for novices to contact browser automation. I hope that in the future, through the learning of various libraries and the mastery of opencv and machine learning, we can solve the problem of man-machine identification and truly simplify the operation of users.

Added by afbase on Wed, 02 Feb 2022 04:12:29 +0200