【问题标题】:can't find android AutoCompleteTextView element找不到 android AutoCompleteTextView 元素
【发布时间】:2019-07-09 09:04:50
【问题描述】:

我无法访问标记的 X 元素。

已经和开发者谈过了,他们说不能在上面放 id 或可访问性标签,他们说是这个元素类型https://developer.android.com/reference/android/widget/AutoCompleteTextView

我会通过 x 和 y 位置执行此操作,但如果我使用硬编码数字,它会在具有不同分辨率的不同设备上中断。

我怎样才能更有效地定位这个元素?

我正在使用 appium 进行测试自动化,但这对于任何测试自动化框架来说都是一个问题。

图片中使用的工具是uiautomatorviewer。

【问题讨论】:

标签: android mobile automated-tests appium uiautomatorviewer


【解决方案1】:

您可以在不硬编码值的情况下使用 X 和 Y 位置,它也可以在其他设备上使用,因为“X”按钮没有定位器,它的内置功能是带有 edittext,所以 android uiautomator 无法访问这个元素

  1. 获取 EditText 的长度
  2. 获取 X 和 Y
  3. 使用(X 的 3/4,Y/2)在触摸操作中传递值

这会点击那个位置,这里你需要根据你的位置改变3/4

【讨论】:

  • 需要检查如何拆分 x 位置以适用于所有分辨率。谢谢
【解决方案2】:

您应该尝试使用元素的类。 driver.findElement(By.className("android.widget.EditText"));

如果出现超过 1 个元素,您可以尝试使用 List<WebElement> e=driver.findElements(By.className("GLButton")); e.get(≤Position>).click();

【讨论】:

  • 我试过了,只得到了 1 个元素,它有整个矩形,不返回 X
【解决方案3】:

您可以使用 Touchaction 类点击 editText 字段中的 Cross 选项。

试试下面的代码

// Identify the x and y co-ordinate for EditText field
    Point elementLoc = driver.findElement(By.xpath("xpath of edittext field")).getLocation();
    int eleX = elementLoc.getX();
    int eleY = elementLoc.getY();

// Identify the width and height of the editText field
    Dimension elementDimen = driver.findElement(By.xpath("xpath of edittext field")).getSize();
    int eleH = elementDimen.getHeight();
    int eleW = elementDimen.getWidth();


// Determine the x and y co-ordinates of cross option    
    int touchX = (int)(eleX + eleW * 0.75D); // 75% from the x co-ordinate of editText field ( x axis position for cross option)
    int touchY = (int)(eleY + eleH * 0.5D);  // 50 % from the y co-ordinate of editText field (y axis position for cross option)

// Tap on the cross option
    TouchAction<?> action = new TouchAction(driver);
    action.tap(PointOption.point(touchX, touchY)).release().perform();

在找到交叉选项的确切位置之前,先用 touchX 和 touchY 变量的偏移值进行实验。

【讨论】:

  • 我在描述I'd do it through x and y position but then it breaks on different devices with different resolutions if I use hardcoded numbers.上明确说过我的问题是元素没有"xpath of edittext field"....
猜你喜欢
  • 1970-01-01
  • 2017-01-06
  • 2019-02-14
  • 2013-12-21
  • 2014-01-27
  • 2018-03-08
  • 2017-08-30
  • 2021-04-24
  • 2017-12-21
相关资源
最近更新 更多