遍历UI¶
Poco提供了非常简单的方式来处理一系列UI交互,直接用for循环进行迭代遍历即可。在for循环中,每次迭代的对象都是一个UI代理,所以可以像之前说的那样,去访问对象的属性和进行对象操作。
The following example shows how to drag all stars to the shell. Yep it is the same example in homepage.

# coding=utf-8
import time
from poco.drivers.unity3d import UnityPoco
poco = UnityPoco()
poco('btn_start').click()
poco(text='drag drop').click()
time.sleep(1.5)
shell = poco('shell').focus('center')
for star in poco('star'):
star.drag_to(shell)
time.sleep(1)
assert poco('scoreVal').get_text() == "100", "score correct."
poco('btn_back', type='Button').click()
Here is another example to iterate over all names of the model.

# coding=utf-8
import time
from poco.drivers.unity3d import UnityPoco
poco = UnityPoco()
for name in poco('plays').offspring('fish').child('name'):
print(name.get_text()) # pearl/shark/balloonfish
下面例子展示了怎么样在商城界面中购买当前屏幕的所有商品。

# coding=utf-8
poco = Poco(...)
bought_items = set()
for item in poco('main_node').child('list_item').offspring('name'):
# get its text value
item_name = item.get_text()
# markdown the bought item
if item_name not in bought_items:
item.click()
bought_items.add(item_name)
更多示例: