Skip to content

Commit

Permalink
add d.xpath(..).child support
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Mar 3, 2021
1 parent b5e5ccd commit dfa51d6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions XPATH.md
Expand Up @@ -57,6 +57,11 @@ def main():

d.xpath('//*[@text="私人FM"]').parent() # 定位到父元素
d.xpath('//*[@text="私人FM"]').parent("@android:list") # 定位到符合条件的父元素

# 包含child的时候,不建议在使用多条件的xpath,因为容易搞混
d.xpath('@android:id/list').child('/android.widget.TextView').click()
# 等价于下面这个
# d.xpath('//*[@resource-id="android:id/list"]/android.widget.TextView').click()
```

>下面的代码为了方便就不写`import``main`了,默认存在`d`这个变量
Expand Down Expand Up @@ -109,6 +114,10 @@ for el in d.xpath('//android.widget.EditText').all():
# 尚未测试的方法
# 点击位于控件包含坐标(50%, 50%)的方法
d.xpath("//*").position(0.5, 0.5).click()

# child操作
d.xpath('@android:id/list').child('/android.widget.TextView').click()
等价于 d.xpath('//*[@resource-id="android:id/list"]/android.widget.TextView').all()
```

### `XMLElement`的操作
Expand Down
6 changes: 6 additions & 0 deletions uiautomator2/xpath.py
Expand Up @@ -404,6 +404,12 @@ def xpath(self, _xpath: Union[list, tuple, str]):
raise TypeError("Unknown type for value {}".format(_xpath))
return self

def child(self, _xpath: str):
if not _xpath.startswith("/"):
_xpath = "/" + _xpath
self._xpath_list[-1] = self._xpath_list[-1] + _xpath
return self

def position(self, x: float, y: float):
""" set possible position """
assert 0 < x < 1
Expand Down

0 comments on commit dfa51d6

Please sign in to comment.