为什么使用 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();
    }

}


↙↙↙阅读原文可查看相关链接,并与作者交流