Appium appium 检测不到界面控件

zqting124 · 2015年04月13日 · 最后由 天天君 回复于 2015年04月13日 · 2786 次阅读

为什么使用 driver.findElementsByClassName("android.widget.TextView") 找不到界面中的控件,返回为 null?明明界面中有不少于一个 TextView 控件。
界面信息如下:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<hierarchy rotation="0">
<node index="0" text="" class="android.widget.FrameLayout" package="com.example.android.apis" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][720,1280]">
<node index="0" text="" class="android.widget.LinearLayout" package="com.example.android.apis" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][720,1280]">
<node index="0" text="" class="android.widget.FrameLayout" package="com.example.android.apis" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,50][720,146]">
<node index="0" text="" class="android.view.View" package="com.example.android.apis" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,50][720,146]">
<node index="0" text="" class="android.widget.FrameLayout" package="com.example.android.apis" content-desc="" checkable="false" checked="false" clickable="true" enabled="false" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[17,50][97,146]">
<node index="0" text="" class="android.widget.ImageView" package="com.example.android.apis" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[25,66][89,130]" />
</node>
<node index="1" text="" class="android.widget.LinearLayout" package="com.example.android.apis" content-desc="" checkable="false" checked="false" clickable="false" enabled="false" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[97,50][291,146]">
<node index="0" text="" class="android.widget.LinearLayout" package="com.example.android.apis" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[97,73][275,122]">
<node index="0" text="API Demos" class="android.widget.TextView" package="com.example.android.apis" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[97,73][275,122]" />
</node></node></node></node>
<node index="1" text="" class="android.widget.FrameLayout" package="com.example.android.apis" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,146][720,1280]">
<node index="0" text="" class="android.widget.ListView" package="com.example.android.apis" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="true" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,146][720,1280]">
<node index="0" text="Accessibility" class="android.widget.TextView" package="com.example.android.apis" content-desc="Accessibility" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,146][720,242]" />
<node index="1" text="Animation" class="android.widget.TextView" package="com.example.android.apis" content-desc="Animation" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,244][720,340]" />
<node index="2" text="App" class="android.widget.TextView" package="com.example.android.apis" content-desc="App" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,342][720,438]" />
<node index="3" text="Content" class="android.widget.TextView" package="com.example.android.apis" content-desc="Content" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,440][720,536]" />
<node index="4" text="Graphics" class="android.widget.TextView" package="com.example.android.apis" content-desc="Graphics" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,538][720,634]" />
<node index="5" text="Media" class="android.widget.TextView" package="com.example.android.apis" content-desc="Media" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,636][720,732]" />
<node index="6" text="NFC" class="android.widget.TextView" package="com.example.android.apis" content-desc="NFC" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,734][720,830]" />
<node index="7" text="OS" class="android.widget.TextView" package="com.example.android.apis" content-desc="OS" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,832][720,928]" />
<node index="8" text="Preference" class="android.widget.TextView" package="com.example.android.apis" content-desc="Preference" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,930][720,1026]" />
<node index="9" text="Text" class="android.widget.TextView" package="com.example.android.apis" content-desc="Text" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,1028][720,1124]" />
<node index="10" text="Views" class="android.widget.TextView" package="com.example.android.apis" content-desc="Views" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,1126][720,1222]" />
</node></node></node></node></hierarchy>

测试用例如下:


import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import android.webkit.WebView;
import io.appium.java_client.AppiumDriver;

import com.android.uiautomator.core.UiSelector;

import java.io.File;
import java.net.URL;
import java.util.Set;

public class AndroidDemoAppTest {
    private AppiumDriver driver;

    @Before
    public void setUp() throws Exception {
        File appDir = new File("F:\\@androidAnalysis\\AppiumForWindows-1.3.4.1\\java-client-master\\src\\test\\java\\io\\appium\\java_client");
        File app = new File(appDir, "ApiDemos-debug.apk");
        DesiredCapabilities capabilities = new DesiredCapabilities();
//        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
        capabilities.setCapability(CapabilityType.PLATFORM, "WINDOWS");
        capabilities.setCapability("app", app.getAbsolutePath());
        capabilities.setCapability("appPackage", "com.example.android.apis");
        capabilities.setCapability("appActivity", ".ApiDemos");

        capabilities.setCapability("platformName", "Android");  
        capabilities.setCapability("deviceName","HUAWEI G700-U00"); 
//        capabilities.setCapability("deviceName","myAVD"); 
        capabilities.setCapability("platformVersion", "4.2.1");  

        driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);       
    }

    @Test
    public void main() throws InterruptedException{
        driver.findElementsByClassName("android.widget.TextView").get(0).click();//此处报错,因为返回的list为空
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
    }

}

共收到 6 条回复 时间 点赞

driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); 创建需要一段延迟时间,你可以加个 sleep,等界面完全加载后,再去 find 试试

#1 楼 @cpfeng0124 好像不是这个原因,我加了 sleep,手机上的界面也加载好了,但还是报同样的错~

用的是 AppiumForWindows-1.3.4.1,莫非是我环境不对?之前用 driver.getContextHandles() 检测 webview 控件也是检测不到

List<WebElement> entries =driver.findElementsByClassName("android.widget.TextView");
        WebElement targetEntry = null;
        for(WebElement entry : entries) {
            if(entry.getText().equals("App")) {
                targetEntry = entry;
                break;
            }
        }
        targetEntry.click();

从 dump 出来的 xml 看到确实有 class 名为android.widget.TextView的元素。而且代码也没看到啥问题。

鉴于你提到之前检测 webview 控件也有问题。建议你:

  1. 换个电脑试试(检查电脑环境有没有问题)。如果换电脑还有问题换个手机/模拟器试试。
  2. 换个 appium 版本试试(检查 appium 本身有没有问题)
  3. 用 UIAutomator 写个用例,看直接用UISelector能否找到元素

#5 楼 @chenhengjie123 换个 API19 的模拟器,然后用 4 楼的代码就可以~貌似是我之前用的 API17 的版本不行

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册