Java &Appium:无法点击元素 [英] Java & Appium: Unable to click element

查看:74
本文介绍了Java &Appium:无法点击元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在测试的 Android 应用程序中,我尝试单击/点击时间选择器.例如,默认情况下,时间选择器显示 08:00AM.我想将小时从 8 改为 7 &然后稍后更改分钟,但 xpath 似乎不起作用.它点击了一些东西,但当我通过代码点击 OK 按钮时,时间仍然显示 08:00AM.如果有人可以帮助我,那么我将能够改变分钟和;下午到上午也是如此.

In the Android app that I am testing, I am trying to click/tap the time picker. For example by default the time picker shows 08:00AM. I want to change the hour from 8 to 7 & then later on change the minutes but the xpath doesn't seem to be working. Its clicking something but the time still shows 08:00AM when I click the OK button via the code. If someone could help me then I will be able to change minutes & PM to AM as well.

这是我用来点击小时选择器以便选择 7 的方法:

Here's what I am using for tapping the hour picker so that 7 shows up selected:

driver.findElement(By.xpath("//android.widget.NumberPicker[0]/android.widget.Button[0]")).click();

图片:

推荐答案

以下是对我有用的解决方案:

Here's the solution that worked for me:

  1. 使用MobileElement.我也在使用 AndroidDriver 而不是 RemoteWebDriver,所以我不知道这是否可行.
  2. 忘记点击().使用 MobileElement 的 tap(int finger, int duration) 方法.我还没有在互联网上找到什么是持续时间(秒、毫秒等,所以这些值只是点击和试用).
  1. Use MobileElement. I am using AndroidDriver also instead of RemoteWebDriver so I do not know whether that will work or not.
  2. Forget click(). Use tap(int fingers, int duration) method of MobileElement. I have not found on the internet what duration is (seconds, milliseconds, etc so these values are just hit & trial).

我已经在下面发布了代码 &有效.它可能需要改进,但现在我很高兴,因为我不是一个铁杆程序员.

I have posted the code below & it worked. It may need refinement but for now I am happy as I am not a hardcore programmer.

现在假设小时是可变的,具体取决于您的测试

Now assume that hour is variable depending upon your test

            // change hour
            @SuppressWarnings("unchecked")
            List<WebElement> buttons = driver.findElementsByClassName("android.widget.Button");
            MobileElement hour = (MobileElement) buttons.get(0);
            List<WebElement> highlights = driver.findElementsByClassName("android.widget.EditText");
            MobileElement hourHighlight = (MobileElement) highlights.get(0); // highlighted hour

            // lets say we need hour = 4

            while (!hourHighlight.getText().equalsIgnoreCase("4"))
            {
                Thread.sleep(100);
                if (hourHighlight.getText().equalsIgnoreCase("4"))
                {
                    break;
                }
                hour.tap(1, 10);
            }

这篇关于Java &amp;Appium:无法点击元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆