Java& Appium:无法点击元素 [英] Java & Appium: Unable to click element
问题描述
在我正在测试的Android应用中,我试图单击/点击时间选择器.例如,默认情况下,时间选择器显示08:00 AM.我想将小时数从8更改为7&.然后稍后更改分钟,但是xpath似乎不起作用.它单击某些东西,但是当我通过代码单击确定"按钮时,时间仍然显示为08:00 AM.如果有人可以帮助我,我将可以更改会议记录&从PM到AM.
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:
- 使用 MobileElement .我也在使用AndroidDriver而不是RemoteWebDriver,所以我不知道这是否行得通.
- 忘记click().使用MobileElement的 tap(手指,整数持续时间)方法.我没有在互联网上找到持续时间是多少(秒,毫秒等,因此这些值仅是按&试用).
- Use MobileElement. I am using AndroidDriver also instead of RemoteWebDriver so I do not know whether that will work or not.
- 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& Appium:无法点击元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!