1

I'd like to automatically run Firefox on login. Subsequently, I'd like to go to my ISP's page and click on the login button, all done automatically.

I do not need to enter the password, since it is stored in the browser.

I am using Ubuntu 12.04. Is this kind of automation possible?

Jacob Vlijm
  • 85,475

1 Answers1

3

Scandalously dirty, but perfectly working

...is the script below.

I tested it to run firefox, opening the AsUbuntu site, then automatically press the link to open my profile page. Since your password is stored in your browser, in your case pressing the button is sufficient to log in.

How it works in practice

15 seconds after login (your Ubuntu user account), the script:

  • opens firefox
  • waits for the window to appear
  • moves to the url you defined
  • moves the mouse to the button's coordinates and presses the button

The script

#!/usr/bin/env python
import subprocess
import time

# --- set the link below
url = "http://askubuntu.com"
# --- set the mouseposition to click on below
xmouse = 858; ymouse = 166
# --- don't change anything below

appcommand = ["firefox", url]

def run(cmd):
    subprocess.Popen(cmd)
    time.sleep(0.2)

def get(cmd):
    return subprocess.check_output(cmd).decode("utf-8").strip()

def run_firefox():
    run(appcommand)
    while True:
        time.sleep(1)
        try:
            pid = get(["pgrep", "firefox"])
        except subprocess.CalledProcessError:
            pass
        else:
            time.sleep(0.1)
            w = [l.split()[0] for l in get(["wmctrl", "-lp"]).splitlines() if pid in l][0]
            break
    return w

w = run_firefox()

cmd1 = ["xdotool", "windowsize", w, "100%", "100%"]
cmd2 = ["xdotool", "mousemove", str(xmouse), str(ymouse)]
cmd3 = ["xdotool", "click", "1"]

for cmd in [cmd1, cmd2]:
    run(cmd)
time.sleep(3)
run(cmd3)

How to setup

  • The script needs both wmctrl and xdotool

    sudo apt-get install xdotool wmctrl
    
  • Copy the script into an empty file, safe it as run_login.py

  • Now the trickiest part:

    • open your browser move to the page to log in
    • place the mouse on the button to be pressed (don't press)
    • press Ctrl+T to open a terminal
    • type the command xdotool getmouselocation
    • read the coordinates and set them in the head section of the script:


      enter image description here

      xmouse = 856; ymouse = 165
      
    • set the url of your login page:

      url = "http://askubuntu.com"
      
  • Test- run it (with no ff window open) by the command:

    python /path/to/run_login.py
    
  • If all works fine, add it to Startup Applications: Dash > Startup Applications > Add. Add the command:

    /bin/bash -c "sleep 15 && python /path/to/run_login.py"
    

Important note

Since the button is clicked on the page by its coordinates, it will only work as long as the pagelayout is unchanged. In case of changes, you need to redefine te location, as set in the head of the script.

Jacob Vlijm
  • 85,475