Skip to content

Fix child_by_text/child_by_description with allow_scroll_search to return False on .exists instead of raising exception - #1161

Draft
codeskyblue with Copilot wants to merge 5 commits into
masterfrom
copilot/fix-child-by-text-exception
Draft

Fix child_by_text/child_by_description with allow_scroll_search to return False on .exists instead of raising exception#1161
codeskyblue with Copilot wants to merge 5 commits into
masterfrom
copilot/fix-child-by-text-exception

Conversation

Copilot AI commented Jan 22, 2026

Copy link
Copy Markdown
Contributor

child_by_text(allow_scroll_search=True).exists raises UiObjectNotFoundError when element not found, inconsistent with standard selector behavior where .exists returns boolean.

# Before: raises UiObjectNotFoundError
d(resourceId="id/listView").child_by_text("Movies", allow_scroll_search=True).exists

# After: returns False
d(resourceId="id/listView").child_by_text("Movies", allow_scroll_search=True).exists  # False

Changes

  • child_by_text() / child_by_description(): Catch UiObjectNotFoundError when allow_scroll_search=True, return UiObject with sentinel selector (text/description="__ui_object_not_found__", instance=sys.maxsize) that evaluates .exists to False
  • Backward compatibility: Exception still raised when allow_scroll_search=False (default)
  • Tests: Added 4 unit tests covering both methods with/without allow_scroll_search, element found/not found cases

Implementation Notes

  • Uses sys.maxsize as sentinel instance to avoid collision with legitimate indices
  • Fixed sentinel text prevents issues with special characters in user input
  • Maintains lazy evaluation: no RPC call until .exists accessed
Original prompt

This section details on the original issue you should resolve

<issue_title>child_by_text(allow_scroll_search=True,...).exists 找不到会抛一个 UiObjectNotFoundError 而不是返回对应的布尔值</issue_title>
<issue_description>在使用 child_by_text 方法并开启 allow_scroll_search=True 时,如果滚动到底部依然没有找到目标元素,代码会直接抛出uiautomator2.exceptions.UiObjectNotFoundError 异常。
我认为这与 u2 中其他选择器的 .exists 行为不一致。通常情况下,调用 .exists 属性时,如果元素不存在,应该直接返回 False,而不是中断程序抛出异常。这样就只能使用 try...except 来捕获这个错误才能判断元素不存在,这不太符合 .exists 的设计初衷。
下图125行报错。

Image</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 4 commits January 22, 2026 06:36
…o return False for .exists when not found

Co-authored-by: codeskyblue <3281689+codeskyblue@users.noreply.github.com>
… initialization

Co-authored-by: codeskyblue <3281689+codeskyblue@users.noreply.github.com>
…reduce test duplication

Co-authored-by: codeskyblue <3281689+codeskyblue@users.noreply.github.com>
…ext, remove unused import

Co-authored-by: codeskyblue <3281689+codeskyblue@users.noreply.github.com>
@sonarqubecloud

Copy link
Copy Markdown

Copilot AI changed the title [WIP] Fix UiObjectNotFoundError in child_by_text method Fix child_by_text/child_by_description with allow_scroll_search to return False on .exists instead of raising exception Jan 22, 2026
Copilot AI requested a review from codeskyblue January 22, 2026 06:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

child_by_text(allow_scroll_search=True,...).exists 找不到会抛一个 UiObjectNotFoundError 而不是返回对应的布尔值

2 participants