线程“主”中的异常 org.openqa.selenium.StaleElementReferenceException:androidx.test.uiautomator.StaleObjectException

问题描述 投票:0回答:1

我试图在 Android TV 上运行它,我看到 Appium 能够点击所有按钮并在文本框中输入数据,除了第一个按钮。我必须点击它然后等待并使用发送键来查看它是否解决了问题但没有运气。有人可以帮我吗。enter image description here

Base.Java

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.MobileCapabilityType;

public class Base {
    
    public static AndroidDriver<AndroidElement> capabalities() throws MalformedURLException {
        
        File file = new File ("src");
        File fs = new File(file,"TVTestApp_ct.apk");
        
        DesiredCapabilities capabalities = new DesiredCapabilities();
        capabalities.setCapability(MobileCapabilityType.AUTOMATION_NAME,"uiautomator2");
        capabalities.setCapability(MobileCapabilityType.DEVICE_NAME, "Automation");
        capabalities.setCapability(MobileCapabilityType.APP, fs.getAbsolutePath());
        AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"),capabalities);
        return driver;
    }

}

HomeScreen.Java

import java.net.MalformedURLException;

import org.openqa.selenium.By;
import org.openqa.selenium.By.ByClassName;
import org.openqa.selenium.StaleElementReferenceException;

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;

public class HomeScreen extends Base {
    
    public static void main(String[] args) throws MalformedURLException, InterruptedException {
        
        AndroidDriver<AndroidElement> driver = capabalities();
        
        driver.findElementById("com.tvexample.tvtestapp:id/editText").sendKeys("1234");
        driver.findElementById("com.tvexample.tvtestapp:id/editText1").sendKeys("1234")         
        
    }


}

日志:

Jun 21, 2021 2:11:47 PM io.appium.java_client.remote.AppiumCommandExecutor$1 lambda$0
INFO: Detected dialect: W3C
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: androidx.test.uiautomator.StaleObjectException
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'TXCDTL20CT1696', ip: '135.70.71.74', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '15.0.2'
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities {app: C:\Users\cg517n\Documents\e..., appPackage: com.tvexample.tvtestapp, automationName: uiautomator2, databaseEnabled: false, desired: {app: C:\Users\cg517n\Documents\e..., automationName: uiautomator2, deviceName: Automation, platformName: android}, deviceApiLevel: 28, deviceManufacturer: unknown, deviceModel: sdk_google_atv_x86, deviceName: emulator-5554, deviceScreenDensity: 320, deviceScreenSize: 1920x1080, deviceUDID: emulator-5554, javascriptEnabled: true, locationContextEnabled: false, networkConnectionEnabled: true, pixelRatio: 2, platform: LINUX, platformName: Android, platformVersion: 9, statBarHeight: 0, takesScreenshot: true, viewportRect: {height: 1080, left: 0, top: 0, width: 1920}, warnings: {}, webStorageEnabled: false}
Session ID: 75f6f7db-21e8-407e-8a53-59271e4e9e02
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:247)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:41)
    at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
    at io.appium.java_client.DefaultGenericMobileElement.execute(DefaultGenericMobileElement.java:45)
    at io.appium.java_client.MobileElement.execute(MobileElement.java:1)
    at io.appium.java_client.android.AndroidElement.execute(AndroidElement.java:1)
    at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:106)
    at HomeScreen.main(HomeScreen.java:18)
java selenium selenium-webdriver appium appium-android
1个回答
0
投票

例子:

private void findUIByText(String t,String b,int n){
    Log.d(TAG,"findUIByText");
    //autoCheckAndClick.autoClick();
    int i = 0;
    UiObject2 object2 = null;
    while (i < 6) {
        i++;
        object2 = mDevice.findObject(
                By.textContains(t)
        );
        try {
            if(object2 != null){
                Log.d(TAG,"not null");
                if(Objects.equals(b, "TRUE")){
                    object2.click(1000);
                }else {
                    object2.click();
                }
                break;
            }

        } catch (StaleObjectException e) {
            Log.d(TAG,"StaleObjectException");
            if (n < 4){
                findUIByText(t,b,n+1);
            }
        }
        toSleep(500);

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.