[Python Tips] Python controls the Chrome browser to open, switch, and close web pages (send exclusive Chrome operation packaging class source code, where is the Chrome browser cookie?)


foreword

This article will show you how to control the browser through Python to realize the basic operations of opening, switching and closing web pages. In addition, for advanced users, do you know where the Chrome browser cookies are? It can be convenient to locate cookies to achieve more operations with login. Of course, using the method in this article, you can also log in in advance, so that you can operate directly without the cumbersome login process. Follow the steps, Xiaobai can also realize the function!

1. When do you need to use Python to control the browser?

Sometimes, we need to operate the browser to complete the access, such as operating Flush through the web page to simulate stock trading, or doing some web page tests. In short, it is too troublesome to click the mouse many times each time. So is there a way to control the operation through the program? Of course, some people have already thought of keyboard wizard software that simulates a keyboard and mouse. These certainly do. But today we are going to talk about a more direct approach.

2. Download the Chrome browser driver file

1. Install Chrome browser and check the version

Check your own Chrome browser version, and download the driver later to see this, otherwise it will not work if it does not match.
insert image description here

2. Download the browser driver file

Then go to the following web page to find the corresponding version (the main version number can be matched)
https://registry.npmmirror.com/binary.html?path=chromedriver/Select
insert image description here
the file to be downloaded according to the operating system, Windows system Just download this file.
insert image description here

3. Unzip to the python compiler directory (the directory where python.exe is located)

Put the decompressed chromedriver.exe into the python compiler directory. If you use a Python virtual environment, put it in the virtual environment directory as well.

3. Python controls Chrome browser (with source code)

1. The operation is divided into two steps

(1) Enter in the CMD command prompt (the specific path depends on the installation location of your browser):

cd C:\Program Files\Google\Chrome\Application\
chrome.exe --remote-debugging-port=9200 --user-data-dir="D:\tempfiles"

(2) Input in python, the following port number must be consistent with the previous one, multiple browsers, just match it yourself:

option.add_experimental_option("debuggerAddress", "127.0.0.1:9200")

Through the cooperation of the above two steps, selenium can be used to take over the currently opened chrome interface.

2. Python controls the complete source code of Chrome browser

Here, selenium is used to control the browser, and it is packaged into classes for easy operation. And using fuzzy search, you can operate the page through the window titlee. Personal originality, add chicken legs here!

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time,os
import datetime


def fuzzy_find(x,_dict):
    # 对x通过键名模糊查找后返回键名,键值
    for key,value in _dict.items(): 
        if x in key:
            return key,value
    return x,None

# 定义一个字典,设置网页标题和url,后面操作会使用到    
url_dict = {
    
    
            '通达信问小达':'https://wenda.tdx.com.cn/site/wenda/index.html',
            '雪球':'https://xueqiu.com/',
            '百度':'http://www.baidu.com',
            }

class Chrome_browser():

    def __init__(self):
        print('start Chrome_browser')
        options = Options()
        options.add_experimental_option("debuggerAddress", "127.0.0.1:9200")
        self.browser = webdriver.Chrome(options=options)
        self.browser.implicitly_wait(8)  # 设置隐式时间等待
        self._max()
        
    def _max(self):
        self.browser.maximize_window()  # 最大化浏览器

    def _get_tab_dict(self):
        tab_dict = {
    
    }
        handles = self.browser.window_handles
        for handle in handles:  # 切换窗口
            # print ('switch to second window', handle)
            # self.browser.close() # 关闭第一个窗口
            self.browser.switch_to.window(handle)  # 切换到第二个窗口
            # print(self.browser.title)
            title = self.browser.title.replace(u'\xa0', '').split('-')[0] # 剔除标题中的'-'字符
            tab_dict[title] = handle
        return tab_dict

    def open_tab(self,url_dict=url_dict):
        for k,v in url_dict.items():
            print('进入'+k,datetime.datetime.now())
            self.browser.switch_to.new_window('tab')
            self.browser.get(v)
            self.browser.refresh()
            time.sleep(2)
            
    def _switch(self, name='', act=''):
        # 包含同时关闭的功能
        tab_dict = self._get_tab_dict()
        print('tab_dict',tab_dict)
        if name != '':
            key,value = fuzzy_find(name,tab_dict)
            # print('key,value',key,value)
            if value != None:
                self.browser.switch_to.window(value)
                if act == 'close':
                    self.browser.close()
                    return False
                return True
        else:
            return False

    def _close(self):
        # 关闭所有窗口,关闭单个由switch完成。
        tab_dict = self._get_tab_dict()
        for k,v in tab_dict.items():  # 切换窗口
            self.browser.switch_to.window(v)
            self.browser.close()

if __name__=='__main__':

    cb = Chrome_browser()
    cb.open_tab()
    cb._switch(name='通达信',act='') # 切换到title为name的窗口,act='close'则切换完同时关闭。
    cb._close() # 关闭所有窗口,关闭单个由switch完成。

4. Where is the Chrome browser cookie? (Pit Avoidance Guide)

For the above operations, you can log in in advance and save the password to realize automatic login.

But if you need to read cookies to complete more advanced operations, please pay attention to the following path. There are many articles on the Internet before, but many of them cannot be used. The key is that the cookie file path has changed:
Before version 96: ./AppData\Local\Google\Chrome\User Data\default\Cookies
After version 96: ./AppData/Local /Google/Chrome/User Data/Default/Network/Cookies

The full path is like (replace Administrator with your own user name):
C:\Users\Administrator\AppData\Local\Google\Chrome\User Data\Default\Network\Cookies


Summarize

There are a lot of tutorials on the Internet about how Python controls the browser to complete various operations. All kinds of filling holes are inevitable. In order to facilitate the operation and avoid stepping on the pit, this article is written for the convenience of everyone.

各种测试,一个周末又报废了,写作不易,有帮助的话,留个言,也提高以下活跃度。

Guess you like

Origin blog.csdn.net/popboy29/article/details/130626648