【问题标题】:Attempts to tap a UIButton in a UITableViewCell using UI Automation fail with "could not be tapped"尝试使用 UI 自动化在 UITableViewCell 中点击 UIButton 失败,并显示“无法点击”
【发布时间】:2026-01-25 17:50:02
【问题描述】:

我有一个使用 UI 自动化测试的 iPhone 应用程序。

我在 UITableViewCell 中有一个按钮,但是当我尝试使用 UI 自动化点击它时,出现以下错误。

Script threw an uncaught JavaScript error: target.frontMostApp().mainWindow().scrollViews()[0].elements()[element_name].tableViews()[0].elements().firstWithPredicate(name contains[c] '*lyn').elements()["detailsButton"] could not be tapped

我在 Interface Builder 中的按钮上启用了辅助功能,并分配了辅助功能标签(和标识符)“detailsButton”。我可以检索按钮元素并验证它是否有效。由于某种原因,我无法点击它。

UIButton 是一个启用了用户交互的圆形矩形按钮。感谢您的任何反馈。

【问题讨论】:

  • 您是否尝试过像 target.delay(3); 这样设置延迟?在点击按钮之前?
  • 如何也发布您的代码,而不是讨论错误消息。
  • 我认为这是一个关于如何将按钮添加到 tableviewcell 的问题 - 发布代码以获得更好的响应
  • 你能再发一张 UIATableCell 展开的照片吗?

标签: iphone ios xcode instruments ui-automation


【解决方案1】:

您可以分配UIButton,但使用圆形矩形按钮除外。您还可以在存在表视图委托方法的控制器类中设置按钮的目标操作。

【讨论】:

    【解决方案2】:

    如果按钮存在,请先尝试断言,使用tuneup_js 进行简单断言。 然后断言按钮是否启用。

    您是否尝试过: target.frontMostApp().mainWindow().scrollViews()[0].elements()[element_name].tableViews()[0].**cells()**.firstWithPredicate("name contains[c] '*lyn'").elements()["detailsButton"]?

    还发布另一张图片,显示 UIATableCell 在 detailsButton 所在的位置展开。

    【讨论】:

      【解决方案3】:

      你必须使用ButtonType=Custom

      【讨论】:

        【解决方案4】:

        我不知道您为什么要使用 UI 自动化。但就我所知,在 tableviewcell 中添加 UIButton-

        1) 只需在UITableViewCell 中添加 UIButton。
        2)设置UIButton's标签值并将目标添加到该按钮。
        3) 在目标按钮方法中,您可以获取UIButton 对象并在那里做您的事情..

        希望对你有所帮助。

        【讨论】:

          【解决方案5】:
          but=[UIButton buttonWithType:UIButtonTypeCustom];
          

          您必须使用自定义按钮类型。

          【讨论】:

          • 你应该解释更多,而不仅仅是发布一行代码。
          【解决方案6】:

          我遇到了同样的问题。在我添加延迟后,它就可以工作了。

          UIATarget.localTarget().delay(1);
          

          【讨论】:

            【解决方案7】:

            你可以在整个窗口上发送一个点击事件:

            function tap_from_window(elem) {
                var mainWindow = target.frontMostApp().mainWindow();
                var elemRect = elem.rect();
                var windowRect = mainWindow.rect();
            
                var xPos = (20 + elemRect.origin.x) / windowRect.size.width;
                var yPos  = (50 + elemRect.origin.y) / windowRect.size.height;
            
                mainWindow.tapWithOptions({tapOffset:{x:xPos, y:yPos}});    
            }
            
            var target = UIATarget.localTarget();
            var window = target.frontMostApp().mainWindow();
            var tableView = window.scrollViews()[0].elements()[element_name].tableViews()[0];
            tap_from_window(tableView.elements().firstWithPredicate(name contains[c] '*lyn'))
            

            像许多与 iOS 相关的东西一样,我不知道为什么不能直接点击该元素。我从这篇文章的代码中改编了上面的函数:scrollToVisible error while testing on a device, UIAutomation

            【讨论】:

              最近更新 更多