Unable to click button in Lua Script with Splash

270 Views Asked by At

This problem is similar in nature to this question, yet my problem still persists after having attempted the provided solution.

I want my Lua script to react to a modal popup in the case that it appears by closing it. However, the final screenshot, keeps showing the modal pop-up thus indicating that it has not been closed.

This is the initial link from which I want to scrape from: 'https://www.goodreads.com/search?utf8=%E2%9C%93&query='

And here is my Lua script:

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

The function returns num_close_btns: 1 and num_modal: 1 thus suggesting that my css-selectors are fine so I can only assume that the mouse_click event is not doing what I expect it to do.

Screenshot of modal popup

1

There are 1 best solutions below

0
On

The following code works for me. I don't know why removing "if" works:

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