python+appium realizes tiktok automatic click slide

introduction

This article is a long and dirty environment, and old fellow who wants to look at the code directly can jump directly to the back.

Required environment

python, I use 3.6 here

Environment configuration required by appium:
jdk1.8.0
android sdk

Simulator: mumu simulator

Environment configuration

simulator

mumu simulator download address
View Android version of simulator
Open the settings and slide down to see about the tablet. Click in to see the Android version

Appium

Appium installation address

jdk installation address
jdk environment configuration tutorial

Android SDK Download

Here I use a domestic one Download site for Android tools , because the official download is slow..


It is recommended to download the tools in 24.4.1. The small partner of window version suggests to download the one I circled

Android SDK environment configuration

1. After installing the SDK, open the directory

2. I found this picture on the Internet when I installed it. I forgot who the author is. I recommend to refer to this for installation
Note: the Android sdk version is downward compatible. You can only install one version higher than your Android version. Of course, you can also install it from 9 to the version of your simulator. No matter how low it is, don't install it

3. After downloading the sdk version, you will arrive. Configure the environment variable and create a new ANDROID_HOME, write your installation address

4. Find Path in the environment variable
newly build
%ANDROID_HOME%\platform-tools
%ANDROID_HOME%\tools
%ANDROID_HOME%\build-tools\28.0.0

5. Open Appium and fill in our jdk and sdk directories

6. Open Appium service and click the search box that looks like the search box after entering the page

7. Configure the parameters of the connection simulator, including the specific parameters
Note: Here I am based on the mumu simulator. If the old fellow is using other simulators, I can only go to Baidu myself.
{
"platformName": "Android",
"deviceName": "127.0.0.1:7555",
"platformVersion": "6.0.1",
"appPackage": "com.ss.android.ugc.aweme",
"appActivity": "com.ss.android.ugc.aweme.splash.SplashActivity",
"noReset": true,
"unicodekeyboard": true,
"resetkeyboard": true
}
Field resolution:
Platform name: operating system
deviceName: current device port number. Which simulator is used? 7555 is the mumu simulator, and the port number of 7555 is in the installation directory emulator \ nemu \ VMS \ myandrovm_ vbox86\myandrovm_ vbox86. Configured in nemu

appPackage: app package name
appActivity: app activity class name

After saving, click start session to finish the environment configuration

Using Appium to get the tiktok button demo

Tiktok: turn on our simulator and install the jitter.

Start our Appium session and we will go to the selection demo page

1. When we click its parent node, we will find that all pages are selected

2. At this time, we have to click into its child nodes one by one and find the key demo we need one by one, such as the search box.
/*
How to get useful information?
*/
Resource id is what we need to locate the result id of the demo location
How to judge whether the key demo can be clicked? If clickable is true, it means that the demo can be clicked
If clickable is false, my idea here is to write it into the try method to simulate the forced click of the mouse
The important thing is that resource-id tiktok changes every time it is updated.

When we get the demo, we start rolling out the code

Code automation Click

Business objectives

  • After entering the jitter, click the search box, enter beauty search, click on user demo, and then click on the user details page to tiktok to click the next user after the user list appears.

1. The first step is to start the service, let appium connect to the simulator, and enter the platform tools under the sdk directory. Enter cmd and enter

adb connect 127.0.0.1:7555
adb devices




2. Code connection simulator

from appium import webdriver

desired_caps = {}
# The third one is equipped with a mobile phone
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '6.0.1'  # It needs to be changed to the corresponding device version
desired_caps['deviceName'] = '127.0.0.1:7555'
# The following two match the information of the app. aapt can check it
desired_caps['appPackage'] = 'com.ss.android.ugc.aweme'  # Tiktok
desired_caps['appActivity'] = '.splash.SplashActivity'
# The following two are equipped with keyboards, which can not be equipped without a keyboard. After use, you need to manually restore the mobile phone keyboard, and appium will not automatically restore it
desired_caps['unicodeKeyboard'] = 'true'  # Use Unicode keyboard
desired_caps['resetKeyboard'] = 'true'  # Reset current input method
# Whether the APP is reset. Without this, it will be reset every time. It must be
desired_caps["noReset"] = "true"

# Here are the remote connection devices
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)

3. Automatically click to enter the user list interface

#Get window size
def get_size():
    x=driver.get_window_size()['width']
    y=driver.get_window_size()['height']
    return x, y
    
def click_info():

    driver.implicitly_wait(10)
    # Click search
    driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.ss.android.ugc.aweme:id/ctd")').click()
    time.sleep(3)
    
    driver.implicitly_wait(10)     # Because the simulator is too laggy, so sleep longer.
    # Click search to enter the content
    shuru = driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.ss.android.ugc.aweme:id/aia")')
    shuru.send_keys(u'beauty')
    shuru = driver.find_element_by_id('com.ss.android.ugc.aweme:id/kqe').click()
    # driver.swipe(shuru)
    #  Because the clickable search button here is false, it is forced to click
    try:
        ActionChains(driver).click(shuru).perform() # Click the confirm location - search key
    except:
        pass
    time.sleep(3)

    driver.implicitly_wait(10)
    # Click the user box
    user = driver.find_elements_by_id('android:id/text1')[2]
    user.click()
    time.sleep(3)

4,

  • After entering the user list interface, we use Appium to check the demo key after tiktok. After that, we find that the clickable event of the user list is false. I also wrote the idea directly, forcing the click directly.
  • After observation, it is found that their resource IDS look the same, which means that the resource ID positioning method above us is useless. Here, I change my idea. After careful observation, I find that there is a boundary at the bottom with their coordinates inside, which means that we can use the coordinates to obtain clicks, nice!



5. Coordinate acquisition

# Slide the screen up
def swipe(time = 1000):
    # Window size
    size = get_size()
    # Time = 1000 ms = 1 second
    # Get the coordinates of the first user list [0209] [810334]
    x1 = int(size[0] * 0)
    x2 = int(size[0] * 1)
    y1 = int(size[1] - 1231)
    y2 = int(size[1] - 1106)
    # click
    time.sleep(1)
    driver.swipe(x1, y1, x2, y2,time)
    time.sleep(3)
    
    # Click back
    driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.ss.android.ugc.aweme:id/l6")').click()
    time.sleep(3)
    
    # Slide the second coordinate on the screen [0351] [810528]
    a1 = int(size(0) * 0)
    a2 = int(size[0] * 1)
    b1 = y1 + 142
    b2 = y2 + 194
    time.sleep(1)
    driver.swipe(x1, y1, x2, y2,time)
    time.sleep(3)
time.sleep(5)

for i in range(1,1000):
    swipe()
    print(i)

summary

Tiktok can be used in business, and python+Mitmproxy can write a real-time monitoring script to get data, plus I can automatically acquire battering data in automation, which should meet some business needs.

If there is a problem with the coordinate click, you can try a few more coordinates.
The complete code is to splice the modules above. If you don't understand anything, please leave a message
If it's helpful to you, you might as well click three times. You're welcome

Keywords: Python Back-end crawler

Added by kts on Sat, 29 Jan 2022 10:19:52 +0200