在 appium 中查找元素

Find element in appium

如何在线性布局中找到具有特定索引的元素,如图所示我有这个元素 android.widget.EditText,它的索引为 3。当我尝试使用搜索找到它时范围:
driver.FindElement(By.XPath("//android.widget.EditText[@index='3']")).SendKeys("123");
我也试过了
driver.FindElementByXPath("//*[@class='android.widget.EditText' and @index='3']").SendKeys("123");

我得到这个异常 {"An element could not be located on the page using the given search parameters."}

如何select这个元素?

您拥有的是 driver.FindElement(By.XPath("//android.widget.EditText[@index='3']")).SendKeys("123");,这是按属性搜索 xpath 的语法,其中 @attribute 是属性。然而,对于索引,它更简单。只要做 driver.FindElement(By.XPath("//android.widget.EditText[3]")).SendKeys("123");

因为涉及到索引,建议你把元素标识放在一个列表中,然后获取索引并点击。试试下面的代码

List<WebElement> list= driver.findElements(By.XPath("identifier"));
System.out.println("size of List= " + list.size());
        list.get(3).click();