无法在 android 中单击移动应用程序上的按钮

Unable to click button on Mobile App in android

我是移动应用程序自动化测试的新手,使用 Appium TestNG.Am 练习自动化亚马逊应用程序,应用程序成功启动,但当我尝试单击登录选项时,它得到:

"FAILED: login org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 0 milliseconds"

    public void login() throws InterruptedException{

        System.out.println("Login check");
        Thread.sleep(3000);
//      String sample = driver.findElementByXPath("//*[@class='android.widget.Button' and @index='5']").getText();
        System.out.println("Next sleep");

//      driver.findElement(By.xpath("//[@class='android.widget.Button' and @index='5']')]")).click();
//      driver.findElement(By.id("in.amazon.mShop.android.shopping:id/sign_in_button")).click();
        driver.findElement(By.xpath("//android.widget.Button[@index='5']")).click();
        Thread.sleep(3000);
        System.out.println("Pass");

    }

图片:

"//android.widget.Button[@index='5']" 无效的 xpath 定位器。如果你想使用索引(但我强烈建议不要这样做),请这样做:

"(//android.widget.Button)[5]"

但在您的情况下,最好是按资源 ID 搜索:

driver.findElement(By.id("sign_in_button")).click();

如果它仍然不起作用,您可以在搜索元素之前打印应用屏幕 xml 结构:

System.out.println(driver.getPageSource())

分析得到的xml是否包含你的按钮及其属性。

请试试这个代码:

MobileElement el1 = (MobileElement) driver.findElementById("in.amazon.mShop.android.shopping:id/sign_in_button");

el1.click();