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定位并点击时,如何确保把弹窗点掉后,再点击控件 #1034

Open
FigersDancing opened this issue Sep 3, 2024 · 1 comment

Comments

@FigersDancing
Copy link

  • uiautomator2的版本号 3.2.0
  • 具体问题描述:
    按照XPATH.md的描述,弹窗监控原理是:
    1.获取到当前界面的XML(通过dump_hierarchy函数)
    2.检查有没有 跳过, 知道了 这两个按钮,如果有就点击,然后回到第一步
    3.检查有没有播放按钮, 有就点击,结束。没有找到在回到第一步,一直执行到查找次数超标。

但是实测发现,按照如下方式启动watcher:
d.xpath.when("跳过").click()
d.xpath.watch_background()
d.xpath('//*[@resource-id="com.map:id/play"]').click()

watcher的逻辑是在后台循环执行。当点击"play"播放按钮时,虽然按钮被弹窗遮挡,仍然会去点击”play“,此时因为”play“被弹窗遮挡,所以没有点击成功,也不会抛异常。
并不会执行像XPATH.md描述的,“检查”play“按钮前,先检查弹窗,如果有弹窗就点击"的逻辑。

@codeskyblue
Copy link
Member

提供一个自行实现的思路

import uiautomator2 as u2

d = u2.connect()
source = d.xpath.get_page_source()
# 处理弹窗
elements = source.find_elements('//*[@text="关闭"')
if elements:
    x, y = elements[-1].center()
    d.click(x, y)
d.xpath('Play').click()

半伪代码仅供参考

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

No branches or pull requests

2 participants