【问题标题】:Unable to click button on Mobile App in android无法在android中的移动应用程序上单击按钮
【发布时间】:2017-11-29 05:46:37
【问题描述】:

我是使用带有 TestNG 的 Appium 测试的移动应用自动化的新手。我正在练习自动化亚马逊应用,应用成功启动,但是当我尝试点击登录选项时,它得到:

“失败:登录 org.openqa.selenium.NoSuchElementException: An 使用给定搜索无法在页面上找到元素 参数。 (警告:服务器没有提供任何堆栈跟踪 信息)命令持续时间或超时:0毫秒”

    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 selenium appium-android


    【解决方案1】:

    "//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是否包含你的按钮,它的属性是什么。

    【讨论】:

    • 到目前为止已经尝试了所有方法,即使您也建议方法。但没有运气进一步进行。请在我的代码下方找到并告诉我一次。
    • // 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.switchTo().frame(5); driver.findElement(By.xpath("(//android.widget.Button)[5]")).click(); // driver.findElement(By.id("sign_in_button")).click();
    • 在这种情况下,我建议在您搜索按钮之前致电driver.getPageSource() 并打印出来(例如System.out.println)。如果有按钮,请检查您获得的 xml 以及您可以搜索它的哪些属性。顺便说一句,您使用的是什么 appium 服务器版本/ java 客户端版本?如果不是 1.7.1/5.0.4 则更新并重试
    • 是的,它在更新 Java 客户端 (5.0.4) Jar 文件后工作。感谢您的宝贵帮助。
    【解决方案2】:

    请试试这个代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      • 2011-07-05
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 2022-11-30
      相关资源
      最近更新 更多