In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is the method of setting the group control effect of python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the method of setting the group control effect of python".
1. Preface
Group control, I believe most people will be no stranger! The impression is that a computer controls multiple devices to complete a series of operations, and more people like to tie it to Hui production!
two。 Prepare for
Install the Android development environment locally to ensure that adb is added to the environment variable
Connect the prepared multiple devices to the computer using a data cable (or via Hub)
View all connected devices through the adb devices command
# below shows that 3 devices are connected to xag:Test xingag$ adb devicesList of devices attached822QEDTL225T7 deviceca2b3455 deviceDE45d9323SE96 device3. Actual combat
Automatic group control takes a keyword search of Xianyu App as an example. The steps include: open the application, click on the search interface, enter the content, and click the search button.
Let's do this in seven steps
1. Obtain the package name of the target application and initialize the Activity
There are many ways to obtain, the mainstream methods include: adb command, parsing APK, third-party APK, barrier-free service
The adb command is recommended here.
# get the package name and initial Activityadb shell dumpsys activity of the running application | grep-I run
Open Xianyu App, enter the above command at the command terminal, and the terminal will display the package name and Activity name.
2. Get all online devices
Through the adb devices command, through the output, filter all the devices connected to the PC side.
# all devices IDdevices = [] def get_online_devices (self): "get all online devices: return:"global devices try: for device_serias_name in exec_cmd (" adb devices "): # filter out the first piece of data and offline devices if" device "in device_serias_name: Devices.append (device_serias_name.split (") [0]) devices = devices [1:] except Exception as e: all devices and quantities connected to print (e) # return devices3, Group control opens the target application
Traverse the device list and use the adb-s device ID shell am start-W command to open the target application respectively
Def start_app (self): "" Open App: return: "" for device in devices: os.popen ("adb-s" + device + "shell am start-W {} / {}" .format (self.packageName, self.home_activity)) print ('waiting for loading to complete.') Sleep (10) 4, package execution steps
To facilitate the management of the device, write each step of the operation to the YAML file, find the element and perform the click action through ID, enter the content in the input box, call the local method and input parameters
Corresponding here: save the UI tree control, find the input box element and perform the click operation, save the UI tree control (the interface has changed), enter text content, view the search button element and perform the click operation
# steps_adb.yaml# package name and Activitypackage_name: com.taobao.idlefishhome_activity: com.taobao.fleamarket.home.activity.InitActivity# execute step steps:-save_ui_tree_to_local: method: save_ui_tree_to_local args:-find_element_and_click: id: com.taobao.idlefish:id/tx_id-save_ui_tree_to_local: method: Save_ui_tree_to_local-input_content: content: Python-find_element_and_click: id: com.taobao.idlefish:id/search_button
It should be pointed out that in order to improve the adaptability of group control, the actual coordinates of the control need to be obtained through the following steps:
Export the control tree of the interface
Parse the control tree XML file and use regular expressions to get the coordinate values of the target control.
Calculate the center point coordinates of the control
The implementation code for obtaining the coordinates of the center point of the element using the control ID is as follows:
Def get_element_position (element_id, uidump_name): "" through the id of the element, using ElementTree, parsing the element control tree, finding the coordinate center point of the element: param element_id: element id For example:: return: element coordinates "" # parse XML tree = ET.parse ('. /.. /% s.xml'% uidump_name) root = tree.getroot () # element to be found result_element = None # print ('number of lookups') Len (root.findall ('. / / node')) # traversal to find the node element # through the element id for node_element in root.findall ('. / / node'): if node_element.attrib ['resource-id'] = = element_id: result_element = node_element break # if the element cannot be found Return empty if result_element is None: print directly ('Sorry! Element not found!') Return None # parse data coord = re.compile (r "d +") .findall (result_element.attrib ['bounds']) # Center point coordinates position_center = int ((int (coord [0]) + int (coord [2])) / 2), int ((int (coord [1]) + int (coord [3])) / 2) return position_center5, distinguishing device
In order to ensure that the execution of the group control script will not cause interference, the device ID should be distinguished as a parameter before each step is executed.
For example, the interface control tree of the control is saved to different names, clicks on the interface, and the command entered is passed to the corresponding device ID as input parameters.
Def save_ui_tree_to_local (dName): "gets the current Activity control tree Save to the local file name fixed as: uidump.xml: param dName: device id: return: "" exec_cmd ("adb-s% s shell uiautomator dump / data/local/tmp/%s.xml"% (dName, dName)) sleep (2) exec_cmd ("adb-s% s pull / data/local/tmp/%s.xml. /..% (dName, dName)) 6, perform steps
Read the execution steps from the YAML file, traverse the step set, and internally traverse the list of devices to ensure that each step is executed separately on each device
# execute step for step in self.steps: # device for device in devices: pass
Then, the device can be operated by matching different operations with the step name.
# Operation name step_name = list (step) [0] if step_name = = 'save_ui_tree_to_local': # Save UI number to local method = step.get (step_name) .get (' method') save_ui_tree_to_local (device) elif step_name = = 'find_element_and_click': element_id = step.get (step_name) .get (' id') # get Coordinate of the element bound_search_input = get_element_position (element_id Device) # Click the element exec_cmd ('adb-s% s shell input tap% s% s'% (device, bound_search_input [0], bound_search_ input [1])) elif step_name = 'input_content': input_content = step.get (step_name). Get (' content') # simulated input exec_cmd ('adb-s% s shell input text% s'% (device) Input_content)) else: print ('other steps') 7. Close the application
After all the operations are completed, you can also traverse the device and use the adb command to close App.
Def stop_all (self): "close App: return:"for device in devices: os.popen (" adb-s "+ device +" shell am force-stop% s "% self.packageName) Thank you for your reading. This is the content of" what is the method for python to set the group control effect ". After the study of this article I believe you have a deeper understanding of what is the method of setting group control effect of python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.