如果 Appium 中没有 text 或 content-desc 作为该元素的属性,如何滚动到具有 resourceId 或 id 的元素

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

我正在尝试执行此脚本,但滚动功能不起作用,我已经重新检查了resourceId,它是正确的,并且当与文本(“”)或描述()一起使用来代替resourceId(“”时,此方法有效”)。但我的应用程序中没有这些属性值。

WebElement element = driver.findElement(AppiumBy.androidUIAutomator("new UiScrollable(new UiSelector().scrollable(true)).scrollIntoView(new UiSelector().resourceId(\""+resourceId+"\"));"));
element.click();

输出显示NoSuchElementException,但滚动功能不起作用,那么很明显会抛出NoSuchElementException,因为所需的元素不在设备屏幕的当前焦点中,无法找到它。

输出:

org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters.
For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Build info: version: '4.10.0', revision: 'c14d967899'
System info: os.name: 'Mac OS X', os.arch: 'aarch64', os.version: '13.4', java.version: '11.0.19'
Driver info: io.appium.java_client.android.AndroidDriver
Command: [32fc3d21-6a7f-46eb-b8a2-99f18bdd920a, findElement {using=-android uiautomator, value=new UiScrollable(new UiSelector().scrollable(true)).scrollIntoView(new UiSelector().resourceId("<masking_the_id>"));}]
Capabilities {appium:automationName: UIAutomator2, appium:databaseEnabled: false, appium:desired: {automationName: UIAutomator2, platformName: ANDROID}, appium:deviceApiLevel: 33, appium:deviceManufacturer: XXXXX, appium:deviceModel: XXXXXXX, appium:deviceName: XXXXXX, appium:deviceScreenDensity: 450, appium:deviceScreenSize: 1080x2408, appium:deviceUDID: XXXXX, appium:javascriptEnabled: true, appium:locationContextEnabled: false, appium:networkConnectionEnabled: true, appium:pixelRatio: 2.8125, appium:platformVersion: 13, appium:statBarHeight: 70, appium:takesScreenshot: true, appium:viewportRect: {height: 2133, left: 0, top: 70, width: 1080}, appium:warnings: {}, appium:webStorageEnabled: false, platformName: ANDROID}
Session ID: 32fc3d21-6a7f-46eb-b8a2-99f18bdd920a
java appium ui-automation nosuchelementexception
3个回答
0
投票

您可以尝试使用appium检查器检查元素并检查它是否能够通过定位器id识别对象。确保使用资源 id 指定整个包名称

com.org.android:id/submitBtn


0
投票

我尝试为此实现新的逻辑

public boolean scrollDownGestureForUnknownElementAction(String element_path){
boolean canScrollMore;
do{
   canScrollMore = (Boolean) ((JavascriptExecutor) driver).executeScript
                    ("mobile: scrollGesture", ImmutableMap.of("direction", "down", "percent", 50.0, "left", 100, "top", 100, "width", 200, "height", 800, "speed", 1500));
      try {
         if (driver.findElement(By.id(element_path)).isDisplayed());
                break;
          }
          catch (NoSuchElementException exception){
                exception.printStackTrace();
                return false;
          }
}while (canScrollMore);
        return canScrollMore;
}

0
投票

这是您的代码,

HashMap<String, String> map1 = new HashMap<>();
map1.put("strategy", "-android uiautomator");
map1.put("selector", "UiSelector().resourceId(\"test123\")");

driver.executeScript("mobile: scroll", map1);

driver.findElement(AppiumBy.androidUIAutomator("UiSelector().resourceId(\"test123\")")).click();
© www.soinside.com 2019 - 2024. All rights reserved.