question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Lua Scripting - Submiting via JS

See original GitHub issue

Hi,

I’ve been having trouble getting a lua script that calls something like:

splash:runjs("$(':submit')[0].click()")

to run. That is, splash doesn’t seem to actually render the result of the page after submission - as if the submit button had been clicked in the first place.

Any idea how to work around this or what I may be doing wrong?

Thanks, Alex

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
kmikecommented, Jun 17, 2015

Aha, you’re right. There are two issues.

  1. WebKit version Splash uses doesn’t provide .click() function for <a> elements - only buttons (<input> elements) work. This may be fixed by https://github.com/scrapinghub/splash/pull/222, but I haven’t checked it.
  2. jQuery .click() method doesn’t allow to follow links - see e.g. http://stackoverflow.com/questions/1694595/can-i-call-jquery-click-to-follow-an-a-link-if-i-havent-bound-an-event-hand.

A workaround (until Splash gets a nice library to do these tasks) is to use window.location = document.getElementById('#link').href:

function main(splash)
  assert(splash:go(splash.args.url))
  splash:runjs("window.location = document.getElementsByTagName('a')[0].href")
  splash:wait(0.5)
  return {
    html = splash:html(),
    png = splash:png(),
    har = splash:har(),
  }
end

Another workaround is to use a library other than jQuery. For example, one can use CasperJS clientutils:

function setup_casperjs(splash)
  -- preload CasperJS client utils.
  -- __utils__ object is compatible with CasperJS
  splash:autoload("https://raw.githubusercontent.com/n1k0/casperjs/master/modules/clientutils.js")
  splash:autoload([[
    window.__utils__ = new ClientUtils({});
  ]])
end

function main(splash)
  setup_casperjs(splash)
  assert(splash:go(splash.args.url))
  assert(splash:runjs("__utils__.click('#link')"))
  splash:wait(0.5)
  return {
    html = splash:html(),
    png = splash:png(),
    har = splash:har(),
  }
end

As an example, I’m loading CasperJS ClientUtils from github; for production it is better to find a CDN URL for autoload or serve it from your own server.

0reactions
kmikecommented, Nov 30, 2016

There are now scraping helpers which allow to select elements, submit forms, etc. Fixed by #490 and #531.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript - communicate with Lua script - Stack Overflow
I have my responsive front-end based on Bootstrap. What I want to do is send command data from Javascript to a Torch Lua...
Read more >
A Beginner's Guide to Web Scripting with Lua and Fengari.
Fengari allows lua to be used as a web-scripting language i.e. we can use lua instead of ... In JS you can print...
Read more >
JsToDef - simple way to send messages from JS to Lua
Native Extension that makes possible to send messages from JavaScript to Lua in HTML5 build. Setup. You can use the JsToDef extension in...
Read more >
Splash Scripts Tutorial — Splash 3.5 documentation
Splash can execute custom rendering scripts written in the Lua programming language. This allows us to use Splash as a browser automation tool...
Read more >
Lua vs JavaScript | Top 10 Differences You Should Know
Lua is one of the cross-platform programming languages which is a small scripting language for designing a web application that is embedded in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found