fix: 元素抓取生成精确选择器,无属性元素用锚点祖先限定;抖音点赞找不到按钮不再双击屏幕
- 元素选择器生成:无 resource-id/text 的元素改用最近有唯一属性祖先限定范围, 并带同 class 兄弟序号消歧(如 //*[@resource-id=".."]/RelativeLayout[3]), 不再给裸 class(如 //android.widget.ImageView 会误匹配其他元素) - 仍无法限定的宽泛选择器标记 broad,前端元素列表显示"⚠宽泛"提示 - 抖音 like 动作:by_element 找不到点赞按钮改为跳过,不再双击屏幕中央(避免误触)
This commit is contained in:
+49
-38
@@ -112,12 +112,16 @@ def get_elements(serial):
|
||||
return False, f"抓取失败: {e}"
|
||||
|
||||
|
||||
def _flatten(node, out, depth=0, path=""):
|
||||
def _flatten(node, out, depth=0, path="", ctx=None, tag_index=0):
|
||||
"""递归扁平化元素树,提取关键属性供前端选择。
|
||||
|
||||
uiautodev dump_hierarchy 返回 Node 格式:
|
||||
{key, name, bounds(归一化浮点), rect({x,y,width,height}), properties({所有属性}), children}
|
||||
其中 properties 包含原始 XML 属性(resource-id/text/content-desc/class/bounds 字符串等)。
|
||||
|
||||
ctx: 最近一个有唯一属性(resource-id/text/content-desc)祖先的 xpath。
|
||||
无唯一属性的元素用它限定范围生成精确选择器,避免裸 class(如 //ImageView)过宽误点。
|
||||
tag_index: 本节点在父节点同 class 兄弟中的序号(1-based),用于消歧。
|
||||
"""
|
||||
if not isinstance(node, dict):
|
||||
return
|
||||
@@ -128,51 +132,58 @@ def _flatten(node, out, depth=0, path=""):
|
||||
if not bounds_str and node.get("rect"):
|
||||
r = node["rect"]
|
||||
bounds_str = f"[{r['x']},{r['y']}][{r['x']+r['width']},{r['y']+r['height']}]"
|
||||
# 关键属性(前端选择器用)
|
||||
|
||||
rid = props.get("resource-id", "")
|
||||
text = props.get("text", "")
|
||||
desc = props.get("content-desc", "")
|
||||
cls = props.get("class", "")
|
||||
tag = cls.split(".")[-1] if cls else "*"
|
||||
|
||||
# 推荐选择器:唯一属性优先;否则用最近锚点祖先限定范围(带同class序号消歧)
|
||||
if rid:
|
||||
suggested = {"type": "xpath", "value": f'//*[@resource-id="{rid}"]'}
|
||||
child_ctx = suggested["value"]
|
||||
elif text:
|
||||
suggested = {"type": "xpath", "value": f'//*[@text="{text}"]'}
|
||||
child_ctx = suggested["value"]
|
||||
elif desc:
|
||||
suggested = {"type": "xpath", "value": f'//*[@content-desc="{desc}"]'}
|
||||
child_ctx = suggested["value"]
|
||||
else:
|
||||
if ctx:
|
||||
seg = f"/{tag}" + (f"[{tag_index}]" if tag_index > 1 else "")
|
||||
suggested = {"type": "xpath", "value": f"{ctx}{seg}"}
|
||||
child_ctx = f"{ctx}{seg}"
|
||||
else:
|
||||
# 无唯一属性且无锚点祖先:裸 class 过宽,标记 broad 让前端提示
|
||||
suggested = {"type": "xpath", "value": f"//{tag}", "broad": True}
|
||||
child_ctx = None
|
||||
|
||||
item = {
|
||||
"depth": depth,
|
||||
"path": path,
|
||||
"name": name,
|
||||
"resource_id": props.get("resource-id", ""),
|
||||
"text": props.get("text", ""),
|
||||
"description": props.get("content-desc", ""),
|
||||
"class": props.get("class", ""),
|
||||
"resource_id": rid,
|
||||
"text": text,
|
||||
"description": desc,
|
||||
"class": cls,
|
||||
"package": props.get("package", ""),
|
||||
"clickable": props.get("clickable", ""),
|
||||
"bounds": bounds_str,
|
||||
# 推荐选择器(优先级:resource-id > text > description > class)
|
||||
"suggested": _suggest_selector(props),
|
||||
# 推荐选择器(resource-id > text > description > 锚点祖先限定class)
|
||||
"suggested": suggested,
|
||||
}
|
||||
out.append(item)
|
||||
# 递归子节点
|
||||
# 递归子节点,同时计算每个子节点在父下同 class 兄弟中的序号
|
||||
children = node.get("children") or []
|
||||
for i, child in enumerate(children):
|
||||
_flatten(child, out, depth + 1, f"{path}/{i}")
|
||||
|
||||
|
||||
def _suggest_selector(attrs):
|
||||
"""根据属性推荐最佳选择器(前端一键填入用)。
|
||||
|
||||
优先返回 xpath(最稳定,uiautomator2 原生支持),
|
||||
其次 resourceId / text / description / class。
|
||||
"""
|
||||
cls = attrs.get("class", "")
|
||||
rid = attrs.get("resource-id", "")
|
||||
text = attrs.get("text", "")
|
||||
desc = attrs.get("content-desc", "")
|
||||
# 优先生成 xpath:resource-id 最稳定
|
||||
if rid:
|
||||
# 简短类名(去掉包前缀)
|
||||
short_cls = cls.split(".")[-1] if cls else "*"
|
||||
return {"type": "xpath",
|
||||
"value": f'//*[@resource-id="{rid}"]'}
|
||||
if text:
|
||||
return {"type": "xpath",
|
||||
"value": f'//*[@text="{text}"]'}
|
||||
if desc:
|
||||
return {"type": "xpath",
|
||||
"value": f'//*[@content-desc="{desc}"]'}
|
||||
if cls:
|
||||
return {"type": "xpath",
|
||||
"value": f'//{cls}'}
|
||||
return {"type": "xpath", "value": "//*"}
|
||||
cprops = child.get("properties") or {}
|
||||
ccls = cprops.get("class", "")
|
||||
ctag = ccls.split(".")[-1] if ccls else "*"
|
||||
same = 1
|
||||
for prev in children[:i]:
|
||||
pcls = (prev.get("properties") or {}).get("class", "")
|
||||
ptag = pcls.split(".")[-1] if pcls else "*"
|
||||
if ptag == ctag:
|
||||
same += 1
|
||||
_flatten(child, out, depth + 1, f"{path}/{i}", child_ctx, same)
|
||||
|
||||
Reference in New Issue
Block a user