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

How to scroll to the element with resourceId or id if you don't have the text or content-desc as attributes of that element in Appium

提问人:Aaditya Awasthi 提问时间:7/2/2023 最后编辑:Aaditya Awasthi 更新时间:11/13/2023 访问量:414

问:

我正在尝试执行此脚本,但滚动函数不起作用,我重新检查了 resourceId,它是正确的,并且此方法在与 text(“<desired_text>”) 或 description(<desired_text>) 一起使用时有效,而不是 resourceId(“</element_id>”)。但我在应用程序中没有这些属性值。

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

评论


答:

0赞 monil 7/4/2023 #1

您可以尝试使用 appium 检查器检查元素,并检查它是否能够通过定位器 ID 识别对象。请确保使用资源 ID 指定整个包名称com.org.android:id/submitBtn

评论

0赞 Aaditya Awasthi 7/8/2023
我确实用包名称传递了整个 resourceId,但它不起作用。我用新的逻辑修改了这个滚动方法。
0赞 monil 7/8/2023
我们必须监控执行。对象可能不在应用程序的视图端口中。您可以使用 Appium 获取对象的坐标并尝试使用它滚动。到达对象在视口中的点后,您将能够访问它。您可以尝试给 stackoverflow.com/questions/58090767/ 的第一个答案...。请务必检查评论
0赞 Aaditya Awasthi 7/8/2023 #2

我试图为此实现新的逻辑

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赞 Community 7/10/2023
正如目前所写的那样,你的答案尚不清楚。请编辑以添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。您可以在帮助中心找到有关如何写出好答案的更多信息。
0赞 Balaji Dinakaran 11/13/2023 #3

这是你的代码,

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();