且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用selenium,appium和C#在datepicker上选择年份

更新时间:2023-02-07 20:46:24

使用scrollto()方法,它将帮助您滚动到要选择的日期。


I am trying to select/click on a hidden element on a date picker of an android app to select a year, because the element is hidden I need to move to the element before clicking, I have tried various approaches but they've not yielded the desired result. On the date picker, the default selected is the present year (2017), if I select the previous year (2016) it works fine. How do I select a year like 1992 since I would have to swipe it to focus before clicking?

Code:

_driver.FindElement(By.Id("com.appzonegroup.dejavuandroid.zoneRevamp:id/dob")).Click();

            int ThisYear = Int32.Parse(_driver.FindElement(By.Id("com.appzonegroup.dejavuandroid.zoneRevamp:id/date_picker_year")).Text);
            String RegistrableYear = Convert.ToString(ThisYear - 25);
            _driver.FindElement(By.Id("com.appzonegroup.dejavuandroid.zoneRevamp:id/date_picker_year")).Click();

            IWebElement ElementToClick = _driver.FindElement(By.XPath("//android.widget.TextView[@text=" +"'"+RegistrableYear+"'"+"]"));
            Actions ActionBuilder = new Actions(_driver);
            Actions HoverAction = ActionBuilder.MoveToElement(ElementToClick).MoveByOffset(GetX(10), GetY(10)).Click();
            HoverAction.Build().Perform();

Error log

> info: [debug] Pushing command to appium work queue: ["find",{"strategy":"xpath","selector":"//android.widget.TextView[@text='1992']","context":"","multiple":false}]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"xpath","selector":"//android.widget.TextView[@text='1992']","context":"","multiple":false}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: find
> info: [debug] [BOOTSTRAP] [debug] Finding //android.widget.TextView[@text='1992'] using XPATH with the contextId:  multiple: false
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":7,"value":"Could not find an element using supplied strategy. "}
> info: [debug] Condition unmet after 20354ms. Timing out.
> info: [debug] Responding to client with error: {"status":7,"value":{"message":"An element could not be located on the page using the given search parameters.","origValue":"Could not find an element using supplied strategy. "},"sessionId":"f4ed7681-9932-42e3-9490-3cc2b556cd11"}

Use scrollto() method and it will help you to scroll to the date which you want to select.