java - 我想知道一种用于滚动和单击菜单每个元素的通用方法
问题描述
我想滚动浏览此应用程序和其他相关应用程序的菜单,也想单击它们。所以请告诉一个可以在 Appium Java 中的所有应用程序上运行的方法
到目前为止,这是我点击所有应用程序的菜单按钮但不滚动或点击的代码:
package project;
import java.util.Scanner;
import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.FileReader;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.touch.TouchActions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.PressesKeyCode;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidKeyCode;
public class firstpro {
static AppiumDriver<MobileElement> driver = null;
public static void main(String[] args) {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("deviceName", "Samsung Galaxy");
caps.setCapability("udid", "HFTUN678");
caps.setCapability("platformName", "Android");
caps.setCapability("platformVersion", "7.0");
caps.setCapability("appPackage", "com.olx.pk");
caps.setCapability("appActivity",
"pl.tablica2.activities.ProxyActivity");
try {
driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), caps);
} catch (MalformedURLException e) {
System.out.println(e.getMessage());
}
menus();
}
public static void menus() {
WebDriverWait wait = new WebDriverWait(driver, 10000);
WebElement Element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("android.view.ViewGroup")));
driver.findElement(By.className("android.widget.ImageButton")).click();
}
}
解决方案
你可以试试这段代码,我不确定它有多准确,因为我仍然不知道完整的节点树结构,但它应该让你对可以采取的一些方法有一个很好的了解:
public static void menus() {
List<WebElement> scrollViews = driver.findElements(By.className("android.widget.ScrollView"));
Iterator<WebElement> it = scrollViews.iterator();
while(it.hasNext()) {
WebElement scrollView = it.next();
// make sure this scroll view has the child element "Home"
List<WebElement> homeElements = scrollView.findElements(By.xpath("*[text() = 'Home' or text() = 'home']"));
if(homeElements.isEmpty()) {
// filter out scroll views that don't have a child with the text "Home"
it.remove();
} else if (homeElements.size() > 1) {
// this scroll view has multiple children with the text "Home"
}
}
if(scrollViews.isEmpty()) {
// TODO error, couldn't find any scroll view containing a child with the text "Home"
} else if (scrollViews.size() > 1) {
// TODO error, there are multiple scroll views containing children with the text "Home"
}
// the only scroll view that has a child with text "Home"
// likely the main menu scroll view
WebElement targetMenuScrollView = scrollViews.get(0);
// get all the ImageButtons in the menu
List<WebElement> menuButtons = targetMenuScrollView
.findElements(By.className("android.widget.ImageButton"));
// click all the buttons
for (WebElement button : menuButtons) {
button.click();
}
}
推荐阅读
- tensorflow - 来自自定义函数的 IoU 得分与来自 Keras 指标的平均 IoU 不匹配
- javascript - Node.js:如何透明地要求和导出子模块
- python - 来自列表字典的字典
- amazon-web-services - aws emr 添加步骤 spark 应用程序
- r - 我如何正确地从 R 中的 NETCDF 文件重新定位 RasterBrick 中放错位置的经度和纬度?
- linux - 如何遍历文本文件(包含一些文件的路径,比如 a、b、c)并复制文本文件中的文件
- c# - Search After Usage API 不返回日志
- sonarqube - 如何将 jacoco 运行时集成报告发布到 Sonarqube
- javascript - 在共享屏幕时单击谷歌浏览器中的停止共享时调用函数
- javascript - 响应式标头未打开或关闭标头