Automating Chrome Automating Chrome google-chrome google-chrome

Automating Chrome


The solution appears to be to make one's own Google Chrome Extension (GCE). It is easy to learn within about 4 hours if you know how to do slightly advanced Javascript stuff, and is very powerful. I can use the Tabs API to create a new tab and go to a specific URL. I can then inject jQuery into that URL and make it manipulate the DOM or do anything we normally can do with jQuery. I can't do file I/O, but there are two workarounds. One, I can force the browser to download a file from a remote location, and I can send data from the current page back up to a remote server via jQuery's $.get() or $.post() calls.


You can use Python to automate web tasks using pywebkitgtk. It is a Python binding for WebKitGtk, which uses the WebKit engine, the same engine as chrome.

Thanks to this blog post, pywebkitgtk - Execute JavaScript from Python, I made a subclass of webkit.WebView to make these tasks easier.

import gtkimport webkitimport jsonclass WebView(webkit.WebView):    def eval_script(self, script):        self.execute_script('oldtitle=document.title;document.title="!!!!";document.title=JSON.stringify(eval(' + json.dumps(script) + '));')        result = json.loads(self.get_main_frame().get_title())        self.execute_script('document.title=oldtitle;')        return result    def wait_for_load(self):        handle = None        def load_status_cb(view, frame):            if frame == view.get_main_frame():                self.disconnect(handle)                gtk.main_quit()        handle = self.connect('load-finished', load_status_cb)        gtk.main()

I added the function called eval_script which is like execute_script, but you could get the results of the function as Python objects. You just need to make sure that what you are evaluating is JSON-serializable.

Also, I added a wait_for_load function which is pretty self-explanatory.

To set up a UI, you first have to create a window, a scrolled window, and a web view.

# windowwindow = gtk.Window()window.set_default_size(800, 600)# scroll viewscroll_view = gtk.ScrolledWindow()scroll_view.props.hscrollbar_policy = gtk.POLICY_AUTOMATICscroll_view.props.vscrollbar_policy = gtk.POLICY_AUTOMATIC# web viewweb_view = WebView()# eventswindow.connect('delete-event', lambda window, event: gtk.main_quit())# showscroll_view.add(web_view)window.add(scroll_view)window.show_all()

Then you can start automating things! For example, this code loads StackOverflow's login page, click the Facebook login button, fills in the username and password (in this case "test"). Finally, it shows the login button text.

# the script is hereweb_view.open('http://www.stackoverflow.com/users/login')web_view.wait_for_load()web_view.execute_script('openid.signin("facebook")')web_view.wait_for_load()web_view.execute_script('document.querySelector("#email").value = "test"')web_view.execute_script('document.querySelector("#pass").value = "test"')print "Login's button text is:", web_view.eval_script('document.querySelector("#buttons input[type=\\"submit\\"]").value')

In my case, the Facebook's interface was in Thai language, and I could see the login's button text.

Login's button text is: เข้าสู่ระบบ

You can also have it actually click the submit button, just by calling click() on that element. (Note: click() works for button elements, not on links)

web_view.execute_script('document.querySelector("#buttons input[type=\\"submit\\"]").click()')web_view.wait_for_load()

You will notice that after all the scripts are finished, the application closes itself without waiting.

If you want to keep the application running after it finish all the scripts in there, you need to add the last line:

gtk.main()

Also, if you remove the window.show_all() line and the last gtk.main() line. Then your app will work without a GUI. (Note: You still need a display server.)

Right now, we don't have good pywebkitgtk docs yet, so you have to look at WebKitGtk's documentation instead. Good luck.


You could try iMacros for Chrome. It is a pretty easy to use automation system.

  1. Open iMacros
  2. Click Record.
  3. Go about you browsing routine.
  4. Click stop.

I don't think it can get any easier than that. The scripts it saves are in plain text so you can edit them for some fine grain control if need be.