如何在 Appium 中处理日期选择器 Android

How to deal with datepicker in Appium Android

我的 android 应用程序正在使用日期选择器,但我无法 select 通过日期选择器进行约会。我在 datepicker 的应用程序中使用了以下代码,但它不起作用。

List<WebElement> pick = driver.findElements(By.className("android.widget.EditText"));           

pick.get(0).sendKeys("21");
pick.get(1).sendKeys("Mar");
pick.get(2).sendKeys("1989");

Swipe 方法将帮助您滚动日历日期,请确保您已将 Java-client JAR 添加到您的项目,然后仅支持 swipe 方法。

示例:

First click on your calendar icon and then use following code :

Thread.sleep(5000);
 for(int y=0;y<3;y++)
    {

        driver.swipe(350,511,350,577,0);  


    }

滑动语法:

driver.swipe(startx, starty, endx, endy, duration);

注意:在上面的代码中,我使用了示例坐标,因此您可以根据需要进行更改。您可以从该日期选择器的绑定值中获取精确坐标。

我在上面的代码中使用了循环,因为我想滑动 3 次,所以如果当前日期是 5 月 1 日,那么它将滑动到 5月4日。

您可以根据需要修改循环。

我已经使用 Xpath 执行日期选择器操作并且它工作正常。

driver.findElement(By.xpath("//android.widget.NumberPicker[@index='0']")).sendKeys("Jan"); driver.findElement(By.xpath("//android.widget.NumberPicker[@index='1']")).sendKeys("24"); driver.findElement(By.xpath("//android.widget.NumberPicker[@index='2']")).sendKeys("1987");

检查 this 是否对

有帮助
driver.FindElement(By.Id("com.eos.eos_du_su:id/ed_manufdate")).Click();
((AndroidElement)(driver.FindElement(By.XPath("//android.widget.NumberPicker[@index='0']//android.widget.Button[@index=0]")))).Tap(1, 2);

((AndroidElement)(driver.FindElement(By.XPath("//android.widget.NumberPicker[@index='1']//android.widget.Button[@index=0]")))).Tap(1, 2);

((AndroidElement)(driver.FindElement(By.XPath("//android.widget.NumberPicker[@index='2']//android.widget.Button[@index=0]")))).Tap(1, 2);

driver.FindElement(By.Id("android:id/button1")).Click();

对于所有仍在寻找 select 日期方式的用户,可以使用以下代码。我正在使用此代码并为我完美地工作。它适用于附加的日历。

do {      
    WebElement source = driver.findElement(By.xpath("//android.view.View[@instance='0']"));    
    WebElement destination = driver.findElement(By.xpath("//android.view.View[@instance='22']"));       
    TouchAction action = new TouchAction((PerformsTouchActions)driver);    
    System.out.println("Dragging item");    
    action.longPress(source).moveTo(destination).release().perform();   
    boolean bul = driver.findElementsByXPath("//android.view.View[@content-desc='24 January 2018']").isEmpty();

  } while(bul!=false);   

driver.findElementByAccessibilityId("24 January 2018").click();

注意: 我使用拖放触摸操作进行滚动,这将向上滚动直到找不到给定日期。我刚刚 select 编辑了同年上个月的日期。您可以对 select 所需的年份使用相同的触摸操作。

我想做同样的事情,但是对于 "calendar" 模式 DatePicker 而不是 "spinner" 模式。这是我的解决方案,对我来说效果很好。

from datetime import datetime

datePickerYearTextViewXpath = "//android.widget.TextView[@resource-id='android:id/date_picker_header_year']"

# initialize your appium driver here
driver = getAppiumDriver()

# define some screen dimensions
screenSize = driver.get_window_size()
halfScreenWidth = screenSize['width'] // 2
halfScreenHeight = screenSize['height'] // 2


def getDatePickerCurrentDate(driver):
    yearTextView = driver.find_element_by_xpath(datePickerYearTextViewXpath)
    yearString = yearTextView.text
    dateTextView = driver.find_element_by_xpath("//android.widget.TextView[@resource-id='android:id/date_picker_header_date']")
    dateString = dateTextView.text
    fullDateString = '{}, {}'.format(dateString, yearString)
    currentDate = datetime.strptime(fullDateString, '%a, %b %d, %Y').date()

    return currentDate


def setDatePickerDate(driver, targetDate):
    # driver is an appium driver
    # targetDate must be a datetime.date, not a datetime

    currentDate = getDatePickerCurrentDate(driver)

    if targetDate.year != currentDate.year:
        yearTextView = driver.find_element_by_xpath(datePickerYearTextViewXpath)
        yearTextView.click()

        # you may need to adjust the following numbers
        # depending on your screen size
        swipeAmountPerYear = 49
        yearsPerScreen = 8
        swipeDuration = 400

        yearOffset = targetDate.year - currentDate.year
        # if target year is older, swipe up (negative)
        swipeDirection = -1 if yearOffset < 0 else 1
        swipeVector = yearsPerScreen * swipeAmountPerYear * swipeDirection
        while True:
            elements = driver.find_elements_by_xpath("//android.widget.TextView[@resource-id='android:id/text1']".format(targetDate.year))
            found = False
            for element in elements:
                if element.text == str(targetDate.year):
                    element.click()
                    found = True
                    break
            if found:
                break
            else:
                driver.swipe(halfScreenWidth, halfScreenHeight, halfScreenWidth, halfScreenHeight - swipeVector, swipeDuration)

    currentDate = getDatePickerCurrentDate(driver)

    if targetDate.month != currentDate.month:
        monthOffset = targetDate.month - currentDate.month
        prevOrNext = 'prev' if monthOffset < 0 else 'next'
        prevOrNextButtonXpath = "//android.widget.ImageButton[@resource-id='android:id/{}']".format(prevOrNext)
        for i in range(abs(monthOffset)):
            driver.find_element_by_xpath(prevOrNextButtonXpath).click()

    targetDateContentDescription = targetDate.strftime('%d %B %Y')
    driver.find_element_by_xpath("//android.view.View[@resource-id='android:id/month_view']/android.view.View[@content-desc='{}']".format(targetDateContentDescription)).click()

    currentDate = getDatePickerCurrentDate(driver)
    if currentDate != targetDate:
        raise ValueError('Unable to set date picker({}) to target date({})!'.format(currentDate, targetDate))