[uiautomation] wechat friend list acquisition (stored in txt)

???

?? Blog homepage: ****

?? Series column: uiautomation

?? Welcome to like?? Comments??

I love python and look forward to growing up with you!!

Purpose: use uiautomation to get wechat friends and store them in txt files.

catalogue

1, Foreword

2, Specific steps

Module import

Open wechat

Enter address book management

Locate specific elements?

Define what is used

Get and store contacts

3, Display of all codes

4, Summary

1, Foreword

Implementation process: as shown below

Open address book management, scroll the mouse continuously to get new contact lists, and add them to the list and txt file one by one.

When the mouse scrolls to the end, press the spacebar to crawl to the last contact list. And print the code running time.

2, Specific steps

Module import

import subprocess
import uiautomation as auto
import time

Open wechat

# ToDo needs to change the address of wechat here. You can right-click the wechat icon on the desktop to find the path where the file is located
subprocess.Popen('E:WeChat WeChatWeChat.exe')

Enter address book management

wechatWindow = auto.WindowControl(searchDepth=1, className='WeChatMainWndForPC', Name='WeChat')

# Click Address Book
button = wechatWindow.ButtonControl(Name='mail list')
button.Click()
# Click Address Book Management
administration = wechatWindow.ButtonControl(Name="Address book management")
administration.Click()

Locate specific elements

list1 in the code is the yellow box in the following figure

communication_administration = auto.WindowControl(Name="Address book management", ClassName="ContactManagerWindow")
# Place the mouse in the center of the content, and the scroll wheel will take effect on the contact list
communication_administration.MoveCursorToMyCenter()
list1 = communication_administration.ListControl(Name="")

**Note: ****communication_administration.MoveCursorToMyCenter() * * means to place the mouse in the center of the content. Because it is placed in the center of the address book management page here, the mouse is on the side of the contact list, so the scroll wheel on the contact list can take effect.

Define what is used

# a indicates the storage list, b indicates the counter, and flag controls whether the code exits
a = []
b = 1

flag = True
start_time = time.time()
print("""Start time:{}""".format(start_time))

The initial time is also defined here, which is convenient for viewing the code running time after the code runs.

Next comes the most important part of this article

Get and store contacts

Put the code first

while flag:
    list1 = communication_administration.ListControl(Name="")
    nickname = list1.GetChildren()[0].TextControl()
    '''Determine whether it is on the list a , if not in the list, add'''
    if nickname.Name not in a:
        print(b, '', nickname.Name)
        b += 1
        a.append(nickname.Name)
        with open('name.txt', 'a', encoding='utf-8') as f:
            f.write(nickname.Name)
            f.write('
')
    # wheel down 
    auto.WheelDown(waitTime=0.01)


    # Manually roll the roller to the bottom
    # Space
    if auto.IsKeyPressed(auto.Keys.VK_SPACE):
        print("In the end")
        for j in list1.GetChildren()[1:]:
            last_nickname = j.TextControl()

            if last_nickname not in a:
                print(b, '', last_nickname.Name)
                b += 1
                a.append(last_nickname)
                with open('name.txt', 'a', encoding='utf-8') as f:
                    f.write(last_nickname.Name)
                    f.write('
')
        print(a)
        flag = False
        end_time = time.time()
        sum_time = end_time - start_time
        print("""Running time:{}s""".format(sum_time))

Explanation: ① here I use the code that has been executed like while True, but here I use flag to control whether the code continues to run, and when the flag is rolled to the bottom of the scroll wheel, press the spacebar to change the flag value, and finally stop running the code.

② First, we locate the first contact displayed each time and judge whether it is in list a. if it is not, add it and save it to the name.txt file;

    list1 = communication_administration.ListControl(Name="")
    nickname = list1.GetChildren()[0].TextControl()
    '''Determine whether it is on the list a , if not in the list, add'''
    if nickname.Name not in a:
        print(b, '', nickname.Name)
        b += 1
        a.append(nickname.Name)
        with open('name.txt', 'a', encoding='utf-8') as f:
            f.write(nickname.Name)
            f.write('
')

③ Whether the judgment is true or not, execute the operation of rolling down the mouse wheel;

    auto.WheelDown(waitTime=0.01)

④ When rolling to the bottom, because the author currently has no good method to judge whether it is at the bottom, the code uses the way of manually pressing the space to let the code know that the scroll wheel is at the bottom, and then traverse all contact elements on the page to judge whether they exist in the list. If not, add them and add them to the name.txt file;

    # Manually roll the roller to the bottom
    # Space
    if auto.IsKeyPressed(auto.Keys.VK_SPACE):
        print("In the end")
        for j in list1.GetChildren()[1:]:
            last_nickname = j.TextControl()

            if last_nickname not in a:
                print(b, '', last_nickname.Name)
                b += 1
                a.append(last_nickname)
                with open('name.txt', 'a', encoding='utf-8') as f:
                    f.write(last_nickname.Name)
                    f.write('
')

auto.IsKeyPressed(auto.Keys.VK_SPACE) means that the keyboard presses a space

⑤ Finally, print list a, change the flag, and print the code running time.

3, Display of all codes

# -*- coding: utf-8-*-
import subprocess
import uiautomation as auto
import time

# ToDo needs to change the address of wechat here. You can right-click the wechat icon on the desktop to find the path where the file is located
subprocess.Popen('E:WeChat WeChatWeChat.exe')

wechatWindow = auto.WindowControl(searchDepth=1, className='WeChatMainWndForPC', Name='WeChat')

# Click Address Book
button = wechatWindow.ButtonControl(Name='mail list')
button.Click()
# Click Address Book Management
administration = wechatWindow.ButtonControl(Name="Address book management")
administration.Click()

communication_administration = auto.WindowControl(Name="Address book management", ClassName="ContactManagerWindow")
# Place the mouse in the center of the content, and the scroll wheel will take effect on the contact list
communication_administration.MoveCursorToMyCenter()
list1 = communication_administration.ListControl(Name="")
# a indicates the storage list, b indicates the counter, and flag controls whether the code exits
a = []
b = 1

flag = True
start_time = time.time()
print("""Start time:{}""".format(start_time))
while flag:
    list1 = communication_administration.ListControl(Name="")
    nickname = list1.GetChildren()[0].TextControl()
    '''Determine whether it is on the list a , if not in the list, add'''
    if nickname.Name not in a:
        print(b, '', nickname.Name)
        b += 1
        a.append(nickname.Name)
        with open('name.txt', 'a', encoding='utf-8') as f:
            f.write(nickname.Name)
            f.write('
')
    # wheel down 
    auto.WheelDown(waitTime=0.01)


    # Manually roll the roller to the bottom
    # Space
    if auto.IsKeyPressed(auto.Keys.VK_SPACE):
        print("In the end")
        for j in list1.GetChildren()[1:]:
            last_nickname = j.TextControl()

            if last_nickname not in a:
                print(b, '', last_nickname.Name)
                b += 1
                a.append(last_nickname)
                with open('name.txt', 'a', encoding='utf-8') as f:
                    f.write(last_nickname.Name)
                    f.write('
')
        print(a)
        flag = False
        end_time = time.time()
        sum_time = end_time - start_time
        print("""Running time:{}s""".format(sum_time))

**Note: * * since it determines whether the contact name already exists in the list, if the contact names are equal, they will not be added, so there will be fewer contacts.

Therefore, you can use the following code segment to try and see whether the results are the same. The following code is to judge whether the new contact is consistent with the contact name recently added to the list. If not, add it.

# -*- coding: utf-8-*-
import subprocess
import uiautomation as auto
import time

# ToDo needs to change the address of wechat here. You can right-click the wechat icon on the desktop to find the path where the file is located
subprocess.Popen('E:WeChat WeChatWeChat.exe')

wechatWindow = auto.WindowControl(searchDepth=1, className='WeChatMainWndForPC', Name='WeChat')

# Click Address Book
button = wechatWindow.ButtonControl(Name='mail list')
button.Click()
# Click Address Book Management
administration = wechatWindow.ButtonControl(Name="Address book management")
administration.Click()

communication_administration = auto.WindowControl(Name="Address book management", ClassName="ContactManagerWindow")
# Place the mouse in the center of the content, and the scroll wheel will take effect on the contact list
communication_administration.MoveCursorToMyCenter()
list1 = communication_administration.ListControl(Name="")
# a indicates the storage list, b indicates the counter, and flag controls whether the code exits
a = []
b = 1

flag = True
start_time = time.time()
print("""Start time:{}""".format(start_time))
while flag:
    list1 = communication_administration.ListControl(Name="")
    nickname = list1.GetChildren()[0].TextControl()
    '''Determine whether it is on the list a , if not in the list, add'''
    if nickname.Name not in a:
        print(b, '', nickname.Name)
        b += 1
        a.append(nickname.Name)
        with open('name.txt', 'a', encoding='utf-8') as f:
            f.write(nickname.Name)
            f.write('
')
    # wheel down 
    auto.WheelDown(waitTime=0.01)


    # Manually roll the roller to the bottom
    # Space
    if auto.IsKeyPressed(auto.Keys.VK_SPACE):
        print("In the end")
        for j in list1.GetChildren()[1:]:
            last_nickname = j.TextControl()

            if last_nickname not in a:
                print(b, '', last_nickname.Name)
                b += 1
                a.append(last_nickname)
                with open('name.txt', 'a', encoding='utf-8') as f:
                    f.write(last_nickname.Name)
                    f.write('
')
        print(a)
        flag = False
        end_time = time.time()
        sum_time = end_time - start_time
        print("""Running time:{}s""".format(sum_time))

4, Summary

① About uiautomation, the wechat contact nickname will be displayed. If there is time next, the code of obtaining comments, labels, etc. will be updated;

② In order to let you know more about uiautomation, the author will update its common functions for your reference;

③ I hope those who are interested in this aspect can knock it by themselves. * * * Don't know if you don't knock it, you can't stop when you knock a bug** If you think the author's writing is good, I hope to support it!!

First of all, I would like to introduce myself. I graduated from Jiaotong University in 13 years. I once worked in a small company, went to large factories such as Huawei OPPO, and joined Alibaba in 18 years, until now. I know that most junior and intermediate Java engineers who want to improve their skills often need to explore and grow by themselves or sign up for classes, but there is a lot of pressure on training institutions to pay nearly 10000 yuan in tuition fees. The self-study efficiency of their own fragmentation is very low and long, and it is easy to encounter the ceiling technology to stop. Therefore, I collected a "full set of learning materials for java development" and gave it to you. The original intention is also very simple. I hope to help friends who want to learn by themselves and don't know where to start, and reduce everyone's burden at the same time. Add the business card below to get a full set of learning materials

Tags: Front-end Android Back-end Interview

Posted by lizzyd on Tue, 09 Aug 2022 13:03:39 +0300