Java & Appium: Unable to click element

£可爱£侵袭症+ 提交于 2019-12-12 02:46:26

问题


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:


回答1:


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



回答2:


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




回答3:


Try this:

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

OR

driver.findElementByXPath("//*[@class='android.widget.button' and @text='7']").click();



回答4:


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:


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

来源:https://stackoverflow.com/questions/32518284/java-appium-unable-to-click-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!