• import unittest
    
    from appium import webdriver
    import desired_capabilities
    
    
    class FindByUIAutomatorTests(unittest.TestCase):
        def setUp(self):
            desired_caps = desired_capabilities.get_desired_capabilities('ApiDemos-debug.apk')
            self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
    
        def tearDown(self):
            self.driver.quit()
    
        #def test_find_single_element(self):
            #el = self.driver.find_element_by_android_uiautomator('new UiSelector().description("Animation")')
            #self.assertIsNotNone(el)
    
        def test_find_multiple_elements(self):
            els = self.driver.find_elements_by_android_uiautomator('new UiSelector().clickable(true)')
            for e in els:
                e.click()
            #self.assertTrue(len(els) > 11)
    
        #def test_element_find_single_element(self):
            #el = self.driver.find_element_by_class_name('android.widget.ListView')
    
            #sub_el = el.find_element_by_android_uiautomator('new UiSelector().description("Animation")')
            #self.assertIsNotNone(sub_el)
    
        #def test_element_find_multiple_elements(self):
            #el = self.driver.find_element_by_class_name('android.widget.ListView')
    
            #sub_els = el.find_elements_by_android_uiautomator('new UiSelector().clickable(true)')
            #self.assertTrue(len(sub_els) > 11)
    
    
    if __name__ == "__main__":
        unittest.main()
    

    ===用官方的代码点击没有效果

  • import os
    import unittest
    from appium import webdriver
    from time import sleep
    
    Returns abs path relative to this file and not cwd
    
    PATH = lambda p: os.path.abspath(
    os.path.join(os.path.dirname(file), p)
    )
    
    desired_caps = {}
    desired_caps['platformName'] = 'Android'
    desired_caps['platformVersion'] = '4.2.2'
    desired_caps['deviceName'] = 'Android Emulator'
    desired_caps['app'] = PATH(
    'D:/Python/appium/ContactManager.apk'
    )
    desired_caps['appPackage'] = 'com.example.android.contactmanager'
    desired_caps['appActivity'] = '.ContactManager'
    
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
    driver.find_element_by_name("Add Contact")
    els = driver.find_elements_by_android_uiautomator('new UiSelector().clickable(true)')
    print els
    driver.quit()