0

How can I use appium to find a element which is not visible in a listview (maybe the element are located in the bottom,and I need to scroll many pages so that i can find the element )

I have used driver.scroll_find_element_by_name() ,but I got a error.

Appium:info: [debug] Didn't get a new command in 60 secs, shutting down...

My code is as follows:

 def scroll_find_element_by_name(self, element_name, time_wait=0.5):
    '''
    @param:
    @rtn: True/False,
    @usage:
    '''
    #
    width,height=self.getScreenResolution()
    for i in range(maxScrollTimes):
        #
        try:
            self.assertRaises(NoSuchElementException, self.driver.find_element_by_name, element_name)
            print "Scroll down " + str(i+1) + ' time to find ' + element_name
        except:
            print 'SUCCESS: ' + element_name + ' found'
            return True

        self.driver.swipe(width / 2, 5 * height / 8, width / 2, 3 * height / 8, 1500)#
        sleep(time_wait)
    print 'UNSUCCESS: ' + element_name + 'NOT found'
    return False

1 Answers1

0

For iOS:

el = self.driver.find_element_by_xpath('xpath_value')
self.driver.execute_script('mobile: scroll', {"element": el, "toVisible": True})

Modify 'find_element_by_xpath' to id or whatever you want it to be.

For Android:

I have written my own custom method which looks for the element - if found it will click else move further down. get_element in the below code snippet is one of my own custom method, you should change it to find_elemeny_by_xpath / id etc. Modify it to suit your needs.

    def android_is_visible(self, locator):
    for _ in xrange(15):
        end_y = 950
        try:
            value = self.get_element(locator).is_displayed()
            if value is True:
                break
        except NoSuchElementException:
            self.driver.swipe(470, 1400, 470, end_y, 400)
            #self.driver.swipe(start_x, start_y, end_x, end_y, duration=400)
            self.driver.implicitly_wait(2)
            continue

Also change value of 'end_y = 950' as per your screen size

akvenk
  • 466
  • 3
  • 8