gpt4 book ai didi

javascript - 飞溅 API/lua 错误 : attempt to index local element (a nil value)

转载 作者:行者123 更新时间:2023-11-29 16:06:12 26 4
gpt4 key购买 nike

我正在编写一个 lua 脚本,我想将其与网站的 scrapy + splash 一起使用。我想写一个输入文本然后点击按钮的脚本。我有以下代码:

function main(splash)
local url = splash.args.url
assert(splash:go(url))
assert(splash:wait(5))

local element = splash:select('.input_29SQWm')
assert(element:send_text("Wall Street, New York"))
assert(splash:send_keys("<Return>"))
assert(splash:wait(5))

return {
html = splash:html(),
}
end

现在我正在使用 splash API 来测试我的代码是否能正常运行。当我点击“渲染!”我收到以下消息:

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

因此,由于某种原因,当我尝试发送“纽约华尔街”时,元素仍然为零。我不明白为什么;如果我在 chrome 控制台中输入以下内容:

$('.input_29SQWm')

我找到了想要的元素!

问:有谁知道我做错了什么?

提前致谢!

最佳答案

正如错误消息告诉您的那样,您尝试索引一个为 nil 的本地“元素”。错误出现在第 7 行:assert(element:send_text("Wall Street, New York"))

那么为什么它是nil?在第 6 行中,我们为 element

赋值
local element = splash:select('.input_29SQWm')

显然 splash:select('.input_29SQWm') 返回 nil

让我们看一下文档:

http://splash.readthedocs.io/en/stable/scripting-ref.html#splash-select

If the element cannot be found using the specified selector nil will be returned. If your selector is not a valid CSS selector an error will be raised.

您的错误是没有处理 select 可能返回 nil 的情况。您不能盲目地索引可能为 nil 的值。此外,在调用引发错误的函数时,您应该使用 protected 调用。

现在由您来找出为什么 select 没有找到使用该选择器的元素。

我建议您在继续之前阅读一些关于 Lua 中的错误处理的内容。

关于javascript - 飞溅 API/lua 错误 : attempt to index local element (a nil value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41622664/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com