SoFunction
Updated on 2024-10-30

Python+Appium Tutorial for Beginners

intend

1. Computer system: win10

2. Cell phone: Android (no money for Apple)

3. The required tools can be downloaded from the official website

  • /
  • /pycharm/
  • /
  • /cn/java/technologies/

mounting

  1. python
  2. jdk
  3. Editor PyCharm
  4. Appium_Python_Client
  5. Android SDK

Installation, the first step to getting your legs under you

python (it can be used for desktop applications, game development, web crawlers)

Check Add Python to PATH (you can manually configure it in the environment variable after successful installation if it is not checked)

Install Now

在这里插入图片描述

Setup...

在这里插入图片描述

Successful installation

在这里插入图片描述

To verify success, type 'python' in the terminal and the following appears as success

在这里插入图片描述

I haven't encountered the Red Sea yet, so if you have any questions, please leave a comment.

jdk

Software Development Kit for the Java Language

appium server seems to support only 1.8 jdk,so sometimes too good to be recognized (dog head to save life)

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

The installation was successful, you need to configure the environment variables in the

在这里插入图片描述

Create a new 'JAVA_HOME'

在这里插入图片描述

Add 'JAVA_HOME' to the path, not replace it, the consequences will be disastrous!

在这里插入图片描述

OK OK OK, open cmd run to test whether the installation is successful, enter 'java -version', success!

在这里插入图片描述

Editor PyCharm

coding

在这里插入图片描述

在这里插入图片描述

Check 64-bit launcher 64-bit launcher

Feel free to check the .py one on my end.

在这里插入图片描述

在这里插入图片描述

successes

在这里插入图片描述

No errors were found during the installation for the moment

Required to run the script, you can view the run log, you can get the app page elements

在这里插入图片描述

在这里插入图片描述

The first run may be a bit slow, just be patient!

在这里插入图片描述

在这里插入图片描述

The three buttons in the upper right corner are

  • "start inspector session".
  • "Get Raw log."
  • "Stop Server".

In layman's terms, "Go for it," "Let me see it," "Don't want to fuck it up."

Here's a piece to look at the log

在这里插入图片描述

Here comes the favorite, the menu bar has a View-Language-Chinese (my baby ah)

在这里插入图片描述

Appium_Python_Client

Calling the Client Library to Communicate with Appium Server

    pip install Appium-Python-Client

Or go to Pypi and download it

在这里插入图片描述

Download and unzip using cmd to enter

importation

python  install

Just run the file.

在这里插入图片描述

Android SDK

The development package of the cell phone baiandroid system is used to execute commands to set up the cell phone, transfer files, install applications, view the cell phone interface and so on.

Just find a feng shui place to decompress.

在这里插入图片描述

All the required tools have been successfully installed, celebrate with an emoticon!

connect a cell phone

Knock on the blackboard, the focus came, do app automation certainly need to use the phone thi, take the usb connection to the computer

(1) Take out your phone ----- open the phone settings ------ find about phone ------- keep clicking on the version number until a prompt appears

在这里插入图片描述

(2) Open the phone's developer options, the open open, the closed, especially the USB debugging here must be opened!

在这里插入图片描述

There are also USB settings that need to be changed, for the time being I've only found these two to be important, so if you're interested you can look into the developer options

在这里插入图片描述

(3) Test if the connection is successful, type 'adb devices -l' in the terminal to check the connected devices, if the list is empty, check if there is any of the above reasons.

在这里插入图片描述

Using python+appium to open a mobile app - Site B

Open PyCharm and appium, directly Start Server Vx,xx,x

Create a new project

在这里插入图片描述

Create a new .py, enter the following code (simply open the app)

# Import webdriver
from appium import webdriver
# Initialization parameters
desired_caps = {
    'platformName': 'Android',  # The phone under test is an Android
    'platformVersion': '10',  # Mobile Android version
    'deviceName': 'xxx',  # Device name, Android phone can be filled in freely
    'appPackage': '',  # Launch APP Package name
    'appActivity': '.',  # Startup Activity Name
    'unicodeKeyboard': True,  # Use your own input method, fill in True for Chinese input
    'resetKeyboard': True,  # Restore the original input method after executing the program
    'noReset': True,  # Don't reset the app, if it is False, after executing the script, the data of the app will be cleared, for example, you were logged in, after executing the script, you quit logging in
    'newCommandTimeout': 6000,
    'automationName': 'UiAutomator2'
}
# Connect to Appium Server and initialize the automation environment
driver = ('http://localhost:4723/wd/hub', desired_caps)
# Exit the program, remember before not knocking this paragraph reported an error Error: socket hang up blah blah blah forget, interested can try one try
()

There are different versions for different cell phones, look it up yourself that's probably what it means

Cell phone android version platformVersion:

在这里插入图片描述

Every app has an appPackage, appActivity, so where should you view it

Open terminal input (cell phone needs to be connected to a computer)

adb shell dumpsys activity recents | find “intent={”

在这里插入图片描述

cmp=/. That's the appPackage, appActivity that we need

appPackage =

appActivity = .

At this point my phone's background looks like this

在这里插入图片描述

Opened successfully, when running it installs stuff on the phone, you need to agree to install it manually, or turn on permissions in the developer options to hear the firecracker

Getting information about an app element

You have to get the element to manipulate it.

Two methods

(1) Using appium

(2) Use uiautomatorviewer (the element positioning tool that comes with the Android SDK)

These two can't be used at the same time, with my current skills I personally recommend using appium

----------------------------- This is a split line ------------------------------------

appiumOpen appium and click Start Inspector Session.

在这里插入图片描述

Enter the corresponding content (that previous code just put in the parameters and values)

在这里插入图片描述

After opening we can see such an interface, the mouse hovering over the mobile interface can see the elements, click on the right side to view the required elements

在这里插入图片描述

uiautomatorviewer(the element positioning tool that comes with the Android SDK)

The previously downloaded Android SDK, open the path: androidsdk\tools\bin you can find the uiautomatorviewer, double click to open it!

在这里插入图片描述

You will get an interface like this

在这里插入图片描述

In the upper left corner of the device Screenshot (uiautomator dump) to get the screen on the phone, if it is a different page need to re-click the

在这里插入图片描述

Hover the mouse and click to see information about this element

在这里插入图片描述

There are some people click will flashback, try the following approach, if not Baidu to go!

I'm just kidding, I'm also Baidu looking for ways to find, looking for a long time did not help, or comment area to leave a message together to solve it (together Baidu)

Open the environment variable and create a new ANDROID_SWT.

My swt is in D:\app\androidsdk\tools\lib\x86_64, just type in the correct path!

在这里插入图片描述

OK OK OK OK, retry double clicking on it

Once you know the element, you can manipulate it

Using python+appium to operate app-B station

Goal: Use Site B to search for 'Titanic'

Commonly used methods to get elements are

find_element_by_id()

find_elements_by_class_name()

find_element_by_xpath()

find_element_by_css_selector()

# Import webdriver
from appium import webdriver
# Initialization parameters
desired_caps = {
    'platformName': 'Android',  # The phone under test is an Android
    'platformVersion': '10',  # Mobile Android version
    'deviceName': 'xxx',  # Device name, Android phone can be filled in freely
    'appPackage': '',  # Launch APP Package name
    'appActivity': '.',  # Startup Activity Name
    'unicodeKeyboard': True,  # Use your own input method, fill in True for Chinese input
    'resetKeyboard': True,  # Restore the original input method after executing the program
    'noReset': True,  # Don't reset the app, if it is False, after executing the script, the data of the app will be cleared, for example, you were logged in, after executing the script, you quit logging in
    'newCommandTimeout': 6000,
    'automationName': 'UiAutomator2'
}
# Connect to Appium Server and initialize the automation environment
driver = ('http://localhost:4723/wd/hub', desired_caps)
# Set the wait time, if you don't give it time you may not find the element
driver.implicitly_wait(5)
# Click on the search box
driver.find_element_by_id("expand_search").click()
# Type in "Titanic" #
driver.find_element_by_id("search_src_text").send_keys("Titanic.")
# Keyboard enter
(66)
# Because it just exits the app after searching and doesn't see the search results page, I gave a way to make him stop
input('**********')
# Exit the program, remember before not knocking this paragraph reported an error Error: socket hang up blah blah blah forget, interested can try one try
()

It ends up looking like this.

在这里插入图片描述

To this Python + Appium tutorial article is introduced to this, more related Python + Appium tutorial content, please search for my previous posts or continue to browse the following related articles, I hope you will support me in the future!