android 如何用java显示所有元素而不仅仅是可见元素

yruzcnhs  于 2023-01-19  发布在  Android
关注(0)|答案(1)|浏览(109)

我想从设置菜单中获取所有名称:https://app.screencast.com/AdssamooNLZbm?conversation=QUFAp0MrtNKXgm68k42Gfy&tab=Details
https://app.screencast.com/J4VoylbceDuca?conversation=Fodt4TO4fN1IVYW4zb9MKH&tab=Details
https://app.screencast.com/U0fnubCAhMjfW?conversation=wluK8dBoc2UkS6YRDqta1u&tab=Details

public static void main(String[] args) throws MalformedURLException, InterruptedException {
               
       DesiredCapabilities dc = new DesiredCapabilities();
       dc.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");   
       dc.setCapability(MobileCapabilityType.DEVICE_NAME, "Redmi Note 9 Pro");     
       dc.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");    
       dc.setCapability(MobileCapabilityType.PLATFORM_VERSION, "12.0");    
       dc.setCapability("appPackage", "com.android.settings");
       dc.setCapability("appActivity", "com.android.settings.Settings");
               
       URL url = new URL("http://127.0.0.1:4723/wd/hub");      
       AndroidDriver<WebElement> driver = new AndroidDriver<WebElement>(url, dc);

       MobileElement list = (MobileElement) driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView("+ "new UiSelector().text(\"Services & feedback\"));"));
       Thread.sleep(5000);
       List <WebElement> list2 = driver.findElementsById("android:id/title");
       System.out.println(list2.size());
       
       for (WebElement i : list2) {
           System.out.println(i.getText());
       }
       }
   }
But my Output: 
10 
Apps
Additional settings
Digital Wellbeing & parental controls
Special features
Mi Account
Google
Accounts & sync
Privacy
Location
Services & feedback
guykilcj

guykilcj1#

从你所提供的来看,它只记录手机上显示的最后10条记录。
首先,对这段代码做一点解释:

MobileElement list = (MobileElement) driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView("+ "new UiSelector().text(\"Services & feedback\"));"));
Thread.sleep(5000);
List<WebElement> list2 = driver.findElementsById("android:id/title");
System.out.println(list2.size());

它滚动到Services & feedback,等待5秒钟,然后捕获所有显示的元素,这些元素具有与List对应的ID。
因此,为了捕获具有所提供ID的所有元素,您可能需要重复以下过程,直到到达目的地:

  • 捕获当前显示元素的名称
  • 执行滚动
// Create a big ArrayList to store all the Elements display name
ArrayList<String> settingNameList = new ArrayList<>();

// Capture the element displayed on screen first
List<WebElement> list1 = driver.findElementsById("android:id/title");
// Add the captured element's title to big List
for (WebElement item : list1) {
  settingNameList.add(item.getText());
}

// Perform the scrolling after first capturing
MobileElement scrollElement = (MobileElement) driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView("+ "new UiSelector().text(\"Services & feedback\"));"));
Thread.sleep(5000);

// Capture again on the current displaying screen
List<WebElement> list2 = driver.findElementsById("android:id/title");
// Add the captured element's title to big List
for (WebElement item : list2) {
  settingNameList.add(item.getText());
}

// Then display the Name one by one
for (String settingName : settingNameList) {
  System.out.println(settingName);
}

请注意,您必须直接处理捕获的WebElement,如果您将WebElement添加到List中进行后续处理,则很可能会遇到StaleElementReferenceException

相关问题