Java & Appium: Unable to click element

2019-09-11 07:29发布

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.

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();

Image:

Screenshot of the UI under test

5条回答
虎瘦雄心在
2楼-- · 2019-09-11 07:53

Instead of clicking the number in NumberPicker, use SendKey.


WebElement hour= driver.findElement(By.xpath("//android.widget.TimePicker[1]/android.widget.NumberPicker[0])); hour.sendKeys("8");

查看更多
该账号已被封号
3楼-- · 2019-09-11 08:04

Here's the solution that worked for me:

  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);
            }
查看更多
唯我独甜
4楼-- · 2019-09-11 08:04

If you want to try javascript what I found worked for Android devices when regular touches and clicks didn't register anything was this:

js.executeScript("$('# pageElement .elementClassName').trigger('touchstart')");

查看更多
戒情不戒烟
5楼-- · 2019-09-11 08:10

I have tried to find an element by Text i.e '7' and it worked for me please try with:

driver.findElementsByName("7").click();

or

helpers.element(By.name("7")).click();

You can also use variable instead of text to click on specific Time

Let me know if it works

查看更多
别忘想泡老子
6楼-- · 2019-09-11 08:14

Try this:

driver.findElementByName("7").click();

OR

driver.findElementByXPath("//*[@class='android.widget.button' and @text='7']").click();
查看更多
登录 后发表回答