How to write scripts for w3m or lynx?

How can I navigate (using TAB or hjkl keys) and click on things(ENTER), enter text with bash script in text based browsers like w3m and lynx? I said text based because when I go graphical, those browsers are going to shred my ram after a long process.

I've searched this thing on google or youtube before asking but what I found is that actualy not what I want. I don't know how to tell kernel "hit ENTER!" or "hit TAB 6 times to go here!" Please help me guys! I need a guide. You can just tell me go learn this or watch this. I saw something like Selenium but those things are so complicated and not exactly what I want.


Solution 1:

lynx has the record and playback flags -cmd_log and -cmd_script. For instance, if you're at the gnu.org homepage and press tab 4 times then hit enter, you go to the German version.

You can record this in Lynx with:

$ lynx -cmd_log=/tmp/gnu-log gnu.org
Looking up  'gnu.org' first
$ cat /tmp/gnu-log
# Command logfile created by Lynx 2.8.8pre.4 (04 Feb 2014)
# Arg0 = lynx
# Arg1 = -cmd_log=/tmp/gnu-log
# Arg2 = gnu.org
key Right Arrow
key Right Arrow
key Left Arrow
key <tab>
key <tab>
key <tab>
key <tab>
key ^J
key q
key <space>

In this script I entered q to quit out of lynx. So this this script will perform some action and then return to command line. On the other hand, if you want to be positioned in the lynx then just trim the last two lines from this /tmp/gnu-log file. Then you can invoke the trimmed file with:

$ lynx -cmd_script=/tmp/gnu-log gnu.org
Looking up  'gnu.org' first

... and you will be positioned on the German gnu.org page in lynx.

Finally, for scripting purposes, just include the lynx -cmd_script with the necessary arguments in a bash script.

There is a full discussion at:

http://blog.unixy.net/2009/06/script-to-automate-browsing-actions-using-lynx/

This solution doesn't allow interactive scripting. For instance, you can't programmatically evaluate a part of a webpage and then do branching operations based on the evaluation. However, it can be useful in a range of situations. You do get easy macro record and playback. Also, you can build up sequences of operations (and of course save webpages) and you can set up cron jobs.