Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xpath单独给一元素定位时无法获取text值? #337

Closed
vitamincqb opened this issue Apr 28, 2019 · 2 comments
Closed

xpath单独给一元素定位时无法获取text值? #337

vitamincqb opened this issue Apr 28, 2019 · 2 comments
Milestone

Comments

@vitamincqb
Copy link

注,代码或者日志请使用Markdown语法,看完删掉

import time
from time import sleep
import logging

import uiautomator2 as u2

d = u2.connect('0123456789ABCDEF')

d.xpath("//android.widget.TextView[1][@resource-id='com.tencent.android.qqdownloader:id/wo']").click()
sleep(2)
d.press('back')
sleep(2)
for x in d.xpath("//android.widget.TextView[@resource-id='com.tencent.android.qqdownloader:id/wo']").all():
    print("Text:", x.text)

# 上面的for循环可以通过text取值,为何单独时就没有text attribute呢?
result = d.xpath("//android.widget.TextView[1][@resource-id='com.tencent.android.qqdownloader:id/wo']")
print(f'result: {result.text}')
“
>>output
Text: 视频
Text: 阅读
Text: 社交
Text: 理财
Text: 购物
Traceback (most recent call last):
  File "C:\Users\q\Desktop\temp.py", line 17, in <module>
    print(f'result: {result.text}')
AttributeError: 'XPathSelector' object has no attribute 'text'

Versions (版本)

Version: 0.2.3.dev2

Issue and steps to reproduce (复现步骤)

上面的for循环可以通过text取值,为何单独时就没有text attribute呢?

result = d.xpath("//android.widget.TextView[1][@resource-id='com.tencent.android.qqdownloader:id/wo']")
print(f'result: {result.text}')

Screenshots (相关截图)

能有尽量有
image
image

Additional Details (其他信息)

@codeskyblue
Copy link
Member

d.xpath("....").all() 返回 list of XMLElement。 暂时可以这么用 d.xpath("....").all()[0].text ,这个确实还没这么好用,以后还需要优化下

@codeskyblue codeskyblue added this to the 0.2.3 milestone Apr 30, 2019
@codeskyblue
Copy link
Member

d.xpath("....").get_text()

计划提供这个方法

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants