无法在 Lua 脚本中使用 Splash 点击按钮

这个问题与这个问题类似,但在尝试了提供的解决方案之后,我的问题仍然存在。

我希望我的 Lua 脚本在出现模态弹出窗口时能够对其进行反应,并关闭它。然而,最终的截图仍然显示模态弹出窗口,表明它尚未关闭。

这是我想要从中爬取内容的初始链接:'https://www.goodreads.com/search?utf8=%E2%9C%93&query='

这是我的 Lua 脚本:

function main(splash, args)
  splash.private_mode_enabled = false
  splash:set_user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0")
  assert(splash:go(args.url))
  assert(splash:wait(0.5))

  input_box = assert(splash:select('#search_query_main'))
  input_box:focus()
  input_box:send_text("dostoevsky")
  input_box:send_keys("<Enter>")
  assert(splash:wait(1))

  if not splash:select('div.modal--centered div.modal__close') == nil then
    close_button = splash:select('div.modal--centered div.modal__close > button')
    close_button.style.border = "5px solid black"
    close_button:mouse_click()
  end
  splash:wait(1)

  return {
    num_modal = #splash:select_all('div.modal--centered div.modal__close'),
    num_close_btns = #splash:select_all('div.modal--centered div.modal__close > button'),
    html = splash:html(),
    png = splash:png(),
  }
end

该函数返回 num_close_btns: 1num_modal: _1_,因此表明我的 CSS 选择器没有问题,所以我只能假定鼠标_click 事件没有做我期望它执行的操作。

模态弹出窗口的截图

点赞
用户21274414
用户21274414

以下代码对我有效。我不知道删除 "if" 为什么有效:

function main(splash, args)
  splash.private_mode_enabled = false
  splash:set_user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0")
  assert(splash:go(args.url))
  assert(splash:wait(0.5))

  input_box = assert(splash:select('#search_query_main'))
  input_box:focus()
  input_box:send_text("dostoevskykqj")
  input_box:send_keys("<Enter>")
  assert(splash:wait(3))

  close_button = splash:select('div.modal--centered div.modal__close > button')
  close_button.style.border = "5px solid black"
  close_button:mouse_click()
  splash:wait(1)

  return {
    num_modal = #splash:select_all('div.modal--centered div.modal__close'),
    num_close_btns = #splash:select_all('div.modal--centered div.modal__close > button'),
    html = splash:html(),
    png = splash:png(),
  }
end
2023-02-24 02:51:52