我的Android应用程序正在使用datepicker,但我无法通过datepicker选择日期。我在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");
答案 0 :(得分:2)
滑动方法将帮助您滚动日历日期,确保您已将Java客户端JAR添加到项目中,然后只支持滑动方法。
示例:
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次,所以如果当前日期第一个可能,那么它将刷到第4个可能。
您可以根据需要修改循环。
答案 1 :(得分:0)
我使用Xpath执行Datepicker操作&amp;它运作正常。
driver.findElement(By.xpath( “// android.widget.NumberPicker [@指数= '0']”))的SendKeys( “一月”); driver.findElement(By.xpath( “// android.widget.NumberPicker [@指数= '1']”))的SendKeys( “24”)。 driver.findElement(By.xpath( “// android.widget.NumberPicker [@指数= '2']”))的SendKeys( “1987”);
答案 2 :(得分:0)
检查this是否有帮助
type alias ViewModel =
{ content : Content
, subView1 : SubViewModel1
, subView2 : SubViewModel2
}
type alias SubViewModel1 =
{ content : Content
, label : Label
, bar : Bar
}
type alias SubViewModel2 =
{ content : Content
, titleLabel : Label
, descLabel : Label
, barbar : BarBar
}
type alias Content =
{ title: String
, description : String
}
type alias LabelModle =
{ value : String
, state : Foo
}
答案 3 :(得分:0)
对于仍在寻找日期选择方式的所有用户,可以使用以下代码。我正在使用此代码,并为我完美地工作。它将适用于附加的日历。
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();
注意::我使用了拖放触摸操作来滚动,如果找不到日期,它将一直滚动。我只是选择了上个月的同一年日期。您可以使用相同的触摸操作来选择所需的年份。
答案 4 :(得分:0)
我想做同样的事情,但是对于“日历”模式DatePicker而不是“旋转器”模式。这是我的解决方案,对我来说一直很好。
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))