Lua脚本失败,但JS可在控制台中运行

时间:2018-12-26 15:02:36

标签: lua scrapy scrapy-splash

我有一个非常基本的lua脚本,该脚本会返回错误,但是直接在控制台中运行querySelector效果很好。

有人暗示我的lua有什么问题吗?

function main(splash, args)
      assert(splash:go(args.url))
      assert(splash:wait(1))
      assert(splash:runjs('document.querySelector("button.btn.btn-primary.btn-show-rates").click()'))
      splash:set_viewport_full()
      return {
        html = splash:html(),
      }
    end

顺便说一句:website is here

错误日志:

{
"type": "ScriptError",
"error": 400,
"info": {
    "type": "LUA_ERROR",
    "line_number": 4,
    "error": "JS error: 'TypeError: null is not an object (evaluating \\'document.querySelector(\"button.btn.btn-primary.btn-show-rates\").click\\')'",
    "message": "Lua error: [string \"function main(splash, args)\r...\"]:4: JS error: 'TypeError: null is not an object (evaluating \\'document.querySelector(\"button.btn.btn-primary.btn-show-rates\").click\\')'",
    "source": "[string \"function main(splash, args)\r...\"]"
},
"description": "Error happened while executing Lua script"
 }

1 个答案:

答案 0 :(得分:0)

原来这很简单...

稍稍较长的“等待”,然后更改为document.querySelectorAll使其起作用。此处的最终代码:

    function main(splash, args)
      assert(splash:go(args.url))
      assert(splash:wait(5.0))
      assert(splash:runjs('document.querySelectorAll("button.btn.btn-primary.btn-show-rates")[0].click()'))
      assert(splash:runjs('document.querySelectorAll("button.btn.btn-primary.btn-show-rates")[1].click()'))
      assert(splash:runjs('document.querySelectorAll("button.btn.btn-primary.btn-show-rates")[2].click()'))
      splash:set_viewport_full()
      return {
        html = splash:html(),
      }
    end