3

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.

savolla
  • 31

2 Answers2

6

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.

1

W3M has some basic macros automation you can script also

enable local cgi-bin folder in w3m config

sed -i 's@cgi_bin.*@cgi_bin ~/.w3m/cgi-bin:/usr/lib/w3m/cgi-bin:/usr/local/libexec/w3m/cgi-bin@g' ~/.w3m/config

create a script

$EDITOR ~/.w3m/cgi-bin/macro_script.cgi && chmod +x ~/.w3m/cgi-bin/macro_script.cgi

echo "w3m-control: TAB_GOTO http://68k.news/" echo "w3m-control: GOTO_LINE 15" echo "w3m-control: NEXT_LINK" echo "w3m-control: GOTO_LINK" echo "w3m-control: NEXT_LINK" echo "w3m-control: GOTO_LINK"

execute from terminal

w3m file:/cgi-bin/macro_script.cgi

this should go to the site, jump to line 15, open on the next link, once the next page loads it will open on the 1st link

for other functions see https://github.com/tats/w3m/blob/master/doc/README.func

gotbletu
  • 11
  • 1