Appium Inspector教程

{
"platformName": "Android",
"appium:deviceName": "Android Device",
"appium:noReset": true,
"appium:automationName": "UiAutomator2"
}

python连接appium

from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy
import time

def connect_appium():
    """连接Appium服务器并打开小红书应用"""
    capabilities = {
        'platformName': 'Android',
        'deviceName': 'Android Device',
        'appPackage': 'com.xingin.xhs',
        'appActivity': '.index.v2.IndexActivityV2',
        'noReset': True,
        'automationName': 'UiAutomator2'
    }
    
    options = UiAutomator2Options().load_capabilities(capabilities)
    driver = webdriver.Remote('http://localhost:4723/wd/hub', options=options)
    time.sleep(10)  # 等待应用加载
    return driver

def locate_recyclerview(driver):
    """定位指定的RecyclerView元素"""
    xpath = '(//androidx.recyclerview.widget.RecyclerView[@resource-id="com.xingin.xhs:id/0_resource_name_obfuscated"])[2]'
    try:
        return driver.find_element(by=AppiumBy.XPATH, value=xpath)
    except Exception as e:
        print(f"定位失败: {str(e)}")
        return None

if __name__ == "__main__":
    driver = None
    try:
        print("连接Appium服务器...")
        driver = connect_appium()
        
        # 定位RecyclerView元素
        print("定位RecyclerView元素...")
        element = locate_recyclerview(driver)
        
        if element:
            print("定位成功!")
            print(f"元素信息: 大小={element.size}, 位置={element.location}")
        
        time.sleep(5)
    except Exception as e:
        print(f"错误: {str(e)}")
    finally:
        if driver:
            driver.quit()
            print("连接已关闭")

posted @ 2025-11-11 21:28  zhangdingqu  阅读(18)  评论(0)    收藏  举报