簡體   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