我有一个非常基本的 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
顺便说一句:网站在这里
错误日志:
{
"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"
}
事实证明这有点简单......
稍微长一点的“等待”并更改为 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
函数 main(splash, args) 断言(飞溅:去(args.url)) 断言(飞溅:等待(5.0)) 断言(splash:runjs('document.querySelectorAll("button.btn.btn-primary.btn-show-rates")[0].click()')) 断言(splash:runjs('document.querySelectorAll("button.btn.btn-primary.btn-show-rates")[1].click()')) 断言(splash:runjs('document.querySelectorAll("button.btn.btn-primary.btn-show-rates")[2].click()')) 启动:set_viewport_full() 返回 { html = 启动:html(), }