无法在appium(python)中找到android中的ImageView元素

时间:2017-10-24 10:22:40

标签: python-2.7 selenium-webdriver appium

我正在尝试在uiautomatorviewer的下方屏幕截图中找到突出显示的按钮。我正在使用该元素的id,但代码给了我NoSuchElementException。我也试过上课,但没有运气。怎么了?

uiautomatorviewer截图 - enter image description here

代码(请检查注释是否有错误的确切行) -

import os
from time import sleep

import unittest
import time
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from appium.webdriver.common.touch_action import TouchAction


# Returns abs path relative to this file and not cwd
PATH = lambda p: os.path.abspath(
    os.path.join(os.path.dirname(__file__), p)
)

class SimpleAndroidTests(unittest.TestCase):
    def setUp(self):
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '7.0'
        desired_caps['deviceName'] = 'mishra'
        desired_caps['app'] = PATH(
            'Shopronto.apk'
        )

        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

    #def tearDown(self):
        # end the session
        #self.driver.quit()
    def test_01_correct_username_correct_password(self):
        print "test1"
        Wait = WebDriverWait(self.driver, 10)
        login_tab=Wait.until(EC.presence_of_element_located((By.ID, "com.shopronto.customer:id/tvLogin")))
        login_tab.click()
        email = self.driver.find_element_by_id("com.shopronto.customer:id/etEmail")
        email.send_keys("user1@gmail.com")
        password = self.driver.find_element_by_id("com.shopronto.customer:id/etPwd")
        password.send_keys("user123")
        self.driver.back()
        login = self.driver.find_element_by_id("com.shopronto.customer:id/tvBottomBtn")
        login.click() 
        Wait.until(EC.presence_of_element_located((By.ID, "com.android.packageinstaller:id/permission_allow_button"))).click()
        #below line gives error
        self.driver.find_element_by_id("com.shopronto.customer:id/ivCategory")


if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(SimpleAndroidTests)
    unittest.TextTestRunner(verbosity=2).run(suite)

当我替换它时 -

self.driver.find_element_by_id("com.shopronto.customer:id/ivCategory")

与 -

        mylist=[]
        self.driver.back()
        mylist = self.driver.find_elements(By.XPATH, "//android.widget.ImageView")

代码保持运行而不提供任何输出(超过15分钟)。

我也试过这个 -

Wait.until(EC.presence_of_element_located((By.XPATH, "//android.widget.ImageView[@index='0']"))).click()

但没有运气。

1 个答案:

答案 0 :(得分:0)

我认为您的所有图片视图都具有相同的ID,因此您应该使用xpath的{​​{1}}找到元素,因为没有带图片视图的文字。