get_attribute方法里参数取值提示元素里没有属性

使用get_attribute方法获取元素属性,提示没有元素的属性。
这是代码:

` from appium import webdriver
import pytest

class Test():
    def setup(self):
        desired = {
            "platformName": "android",
            "deviceName": "127.0.0.1:21503",
            "appPackage": "com.xueqiu.android",
            "appActivity": ".common.MainActivity",
            "platformVersion": "7.1.2",
            "noReset":"true",
            "dontStopAppOnReset":"true",
            "skipDeviceInitialization":"true",
            "unicodeKeyboard":"true",
            # 启用Unicode输入,默认为false
            "resetKeyboard":"true"}

        self.driver = webdriver.Remote("127.0.0.1:4723/wd/hub",desired)
        self.driver.implicitly_wait(10)

    def teardown(self):
        pass

    def test_get_attr(self):
        print("获取元素的属性")
        search_ele = self.driver.find_element_by_id("com.xueqiu.android:id/action_message")
        # search_ele.get_attribute("content-desc")
        print(search_ele.get_attribute("displayed"))
        print(search_ele.get_attribute("content-desc"))
        print(search_ele.get_attribute("enabled"))
        print(search_ele.get_attribute("clickable"))

if __name__ == '__main__':
    pytest.main() `

pycharm报错信息:
E selenium.common.exceptions.NoSuchElementException: Message: This element does not have the 'content-desc' attribute

appium-server日志:

[W3C] Calling AppiumDriver.getAttribute() with args: [“displayed”,“1”,“e225fbba-2711-4e7e-b907-97cb87c99e7a”]
[AndroidBootstrap] Sending command to android: {“cmd”:“action”,“action”:“element:getAttribute”,“params”:{“attribute”:“displayed”,“elementId”:“1”}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“action”,“action”:“element:getAttribute”,“params”:{“attribute”:“displayed”,“elementId”:“1”}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: getAttribute
[AndroidBootstrap] Received command result from bootstrap
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {“status”:0,“value”:“true”}
[W3C] Responding to client with driver.getAttribute() result: “true”
[HTTP] ← GET /wd/hub/session/e225fbba-2711-4e7e-b907-97cb87c99e7a/element/1/attribute/displayed 200 548 ms - 16
[HTTP]
[HTTP] → GET /wd/hub/session/e225fbba-2711-4e7e-b907-97cb87c99e7a/element/1/attribute/content-desc
[HTTP] {}
[W3C] Calling AppiumDriver.getAttribute() with args: [“content-desc”,“1”,“e225fbba-2711-4e7e-b907-97cb87c99e7a”]
[AndroidBootstrap] Sending command to android: {“cmd”:“action”,“action”:“element:getAttribute”,“params”:{“attribute”:“content-desc”,“elementId”:“1”}}
[AndroidBootstrap] Received command result from bootstrap
[MJSONWP] Matched JSONWP error code 7 to NoSuchElementError
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“action”,“action”:“element:getAttribute”,“params”:{“attribute”:“content-desc”,“elementId”:“1”}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: getAttribute
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {“status”:7,“value”:“This element does not have the ‘content-desc’ attribute”}
[W3C] Encountered internal error running command: NoSuchElementError: This element does not have the ‘content-desc’ attribute
[W3C] at errorFromMJSONWPStatusCode (C:\Users\dell\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\appium-base-driver\lib\protocol\errors.js:786:12)
[W3C] at Socket. (C:\Users\dell\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\appium-android-driver\lib\bootstrap.js:139:18)
[W3C] at emitOne (events.js:116:13)
[W3C] at Socket.emit (events.js:211:7)
[W3C] at addChunk (_stream_readable.js:263:12)
[W3C] at readableAddChunk (_stream_readable.js:246:13)
[W3C] at Socket.Readable.push (_stream_readable.js:208:10)
[W3C] at TCP.onread (net.js:594:20)
[HTTP] ← GET /wd/hub/session/e225fbba-2711-4e7e-b907-97cb87c99e7a/element/1/attribute/content-desc 404 5 ms - 834

仅元素的displayed属性可获取。其余都提示该元素没有属性值。求教~

你用uiautomatorviewer定位到那个元素之后截图看看是不是没有content-desc属性的


有的,但是应该返回None才对呀。

你定位的是这个元素,和你截图里要获取属性的元素id不同的喔

嗯呢,截图错了。下面这个是对应的。

我这边使用安卓6的真机是能获取到的,可以加个等待时间看看,如果再不行的话换个版本试试

前面有设置隐式等待了,应该与时间没有关系。
存在两种获取方式时,可以换一种名称获取,就可以了。



谢谢啦~

1 个赞