You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复Ruby/Appium中使用new UiSelector()定义动态选择器的报错?

问题修复方案

错误原因清晰:UiAutomator的resourceId方法要求传入字符串类型参数,但你生成的选择器里tab-button-about未被引号包裹,被解析器判定为未定义标识符,而非合法字符串。

修复步骤

修改Main类中的tab_button方法,为resourceId的参数添加双引号:

class Main
  
  def tab_button
    # 给resourceId的参数加上双引号,确保是合法字符串格式
    @tab_button = Elements.new(:uiautomator, "new UiSelector().resourceId(\"tab-button-#{@tab}\")")
  end

  def switch_to_tab(tabName) 
    @tab = tabName.downcase  
    tab_button 
    @tab_button.click
  end

end

修复原理

修改后生成的选择器表达式会变为:
new UiSelector().resourceId("tab-button-about")
此时UiAutomator能正确识别"tab-button-about"是字符串参数,匹配resourceId方法的参数要求,即可解决解析错误。

内容的提问来源于stack exchange,提问作者Gintars Lazda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 01:10:55