uiautomator2的NoSuchElementException

问题描述 投票:2回答:1

我正在尝试在Sauce Labs上运行我的Python代码,并且它没有设置automationName功能(根据Appium默认为http://appium.io/docs/en/writing-running-appium/caps/)。但是,当我将此功能设置为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:[email protected]: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()
python appium uiautomator saucelabs
1个回答
2
投票

在所需的Some Text元素之前出现的元素(在下面定义)的不可见性上使用显式等待解决了该问题。奇怪的是,在默认的automationName的情况下,不需要这种明确的等待。

wait = WebDriverWait(self.driver, 20)
wait.until(EC.invisibility_of_element_located((By.ID, "com.xxx.abc.my_app:id/prior_element")))

PS:虽然我理解这个答案可能会得到一些支持,但我坚信这是来自UiAutomator2引擎的奇怪行为。

© www.soinside.com 2019 - 2024. All rights reserved.