uiautomator2的NoSuchElementException

时间:2018-08-28 07:03:48

标签: python appium uiautomator saucelabs

我正在尝试在Sauce Labs上运行我的Python代码,并且在未设置automationName功能(根据http://appium.io/docs/en/writing-running-appium/caps/将其默认设置为Appium)下可以正常工作。但是,当我将此功能设置为UiAutomator2时,它会在行element_some_text = self.driver.find_element_by_xpath("//android.widget.TextView[@text='Some Text']")的下面抛出错误:

NoSuchElementException: Message: An element could not be located on the page using the given search parameters.

这是我的代码:

import lemoncheesecake.api as lcc
from appium import webdriver


@lcc.suite("My test suite")
class my_test_suite:
    caps = {}
    driver = None

    def setup_suite(self):
        self.caps['appiumVersion'] = "1.8.1"
        self.caps['deviceName'] = "Android GoogleAPI Emulator"
        self.caps['deviceOrientation'] = "portrait"
        self.caps['platformVersion'] = "7.1"
        self.caps['platformName'] = "Android"
        self.caps['automationName'] = 'uiautomator2'
        self.caps['autoGrantPermissions'] = True
        self.caps['app'] = 'https://somesite.com/storage/my_app.apk'
        self.caps['appPackage'] = 'com.xxx.abc.my_app'
        self.driver = webdriver.Remote(
            'http://username:passkey@ondemand.saucelabs.com:80/wd/hub', self.caps)

    @lcc.test("My app test")
    def verify_app_launch(self):
        self.driver.implicitly_wait(10)
        element_some_text = self.driver.find_element_by_xpath("//android.widget.TextView[@text='Some Text']")
        element_some_text.click()

    def teardown_suite(self):
        self.driver.quit()

1 个答案:

答案 0 :(得分:2)

通过显式等待在所需的ulimit元素之前出现的元素(定义如下)的不可见性,解决了该问题。 虽然很奇怪,但是在默认Some Text的情况下,不需要显式等待。

automationName

PS:虽然我知道这个答案可能会引起一些反对,但我坚信这是wait = WebDriverWait(self.driver, 20) wait.until(EC.invisibility_of_element_located((By.ID, "com.xxx.abc.my_app:id/prior_element"))) 引擎的奇怪行为。