繁体   English   中英

Lua:“错误”:“尝试调用方法'mouse_click'(一个零值)”,

[英]Lua: "error": "attempt to call method 'mouse_click' (a nil value)",

我是 Lua 的新手,并尝试使用“for”循环遍历“items”中的所有链接,并使用 mouse_click() function 单击所有链接。 但它抛出“错误”:“尝试调用方法'mouse_click'(一个零值)”我该怎么做? 我自己尝试了下面提到的代码,但可能缺少一些东西。 从文档中,我发现 Lua 有两种类型的 for 循环。 数字和通用。 但是要遍历链接,然后单击它我必须使用什么?

再一次,我对 Lua 很陌生。 只是想熟悉它,所以我正在寻求所有专家的指导。

function main(splash, args)
  
  splash:set_user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36")
  splash.private_mode_enabled = false
  assert(splash:go(args.url))
  assert(splash:wait(3))
  
  items = assert(splash:select("div.item-list-content a"))
  
  for item in pairs(items) do
    item:mouse_click()
    assert(splash:wait(5))
  end
  
  splash:set_viewport_full()
  return {
    html = splash:html(),
    png = splash:png(),
    har = splash:har(),
  }
end

Output

{
    "error": 400,
    "type": "ScriptError",
    "description": "Error happened while executing Lua script",
    "info": {
        "source": "[string \"function main(splash, args)\r...\"]",
        "line_number": 11,
        "error": "attempt to call method 'mouse_click' (a nil value)",
        "type": "LUA_ERROR",
        "message": "Lua error: [string \"function main(splash, args)\r...\"]:11: attempt to call method 'mouse_click' (a nil value)"
    }
}

Select只返回一个元素,因此使用pairs迭代它没有意义(因为您将迭代该元素的字段,这可能不是您想要的)。 您可能想要使用select_all然后使用 ipairs 来遍历匹配选择器的元素。 有关详细信息和示例,请参阅select文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM