uiautomator2的NoTouchElementException

2024-04-18 07:04:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在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: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()

Tags: 代码texttestselfcomappmydef
1条回答
网友
1楼 · 发布于 2024-04-18 07:04:13

在必需的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引擎的一个奇怪行为。在

相关问题 更多 >