Lua脚本点击按钮失败

我正在尝试使用scrapy-splash从链接中爬取航班,并使用此lua脚本:

function main(splash)
                local waiting_time = 2

                -- 前往URL
                assert(splash:go(splash.args.url))
                splash:wait(waiting_time)

                -- 点击“Outgoing”选项卡
                local outgoing_tab = splash:select('#linkRealTimeOutgoing')
                outgoing_tab:mouse_click()
                splash:wait(waiting_time)

                -- 点击“More Flights”按钮
                local more_flights_btn = splash:select('#ctl00_rptOutgoingFlights_ctl26_divPaging > div.advanced.noTop > a')
                more_flights_btn:mouse_click()
                splash:wait(waiting_time)

                return splash:html()
end

不知何故,我收到了以下错误:

'LUA_ERROR', 'message': 'Lua error: [string "..."]:16: attempt to index local \'more_flights_btn\' (a nil value)', 'error': "attempt to index local 'more_flights_btn' (a nil value)"}, 'type': 'ScriptError', 'description': 'Error happened while executing Lua script'}

有谁知道这是为什么吗? 此外,有谁知道我可以在splash中使用lua脚本的教程?除了官方网站之外。

提前致谢!

点赞
用户4082726
用户4082726

这似乎只是一个时间问题。我运行了你的 Lua 脚本几次,只有一次出现了这个错误。

在获取按钮之前等待更长的时间应该就足够了。但是,如果所需时间变化很大,而且您不总是想等待整个时间,那么您可以尝试像这样稍微更智能的循环:

-- 点击“更多航班”按钮
local more_flights_btn
-- 等待最多 10 秒:
for i=1,10 do
    splash:wait(1)
    more_flights_btn = splash:select('#ctl00_rptOutgoingFlights_ctl26_divPaging > div.advanced.noTop > a')
    if more_flights_btn then break end
    -- 如果未找到,则继续等待。
end
2019-02-21 17:59:56