Archive

Posts Tagged ‘splinter’

Splinter: open Firefox in fullscreen mode

May 17, 2013 1 comment

Problem
With Splinter you can automate a browser window (click on a button, type in some text, etc). You can also use a Firefox instance beside Chrome and some other browsers. But how to open the Firefox instance in fullscreen (as if you had clicked on the “maximize” button)? Strangely, there is no command-line option for this :(

Solution
Well, under Linux there are some tools that allows you to interact with windows:

  • xwininfo
  • xdotool
  • wmctrl

When the Firefox instance is opened, it becomes the active window and I ask its window ID with “xdotool getactivewindow”. Then, with “wmctrl” I can toggle this window to fullscreen.

Demonstration:

jabba@jabba-uplink:~$ xdotool getactivewindow
109051940
jabba@jabba-uplink:~$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01) 
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> hex(109051940)
'0x6800024'
jabba@jabba-uplink:~$ wmctrl -i -r 0x6800024 -b toggle,maximized_vert,maximized_horz

The same in Python is available in my jabbapylib library here.

Categories: python Tags: , ,

Splinter patch: open the Chrome browser window in a maximized way

Problem
With Splinter, I would like to open the Chrome browser window in a maximized way, i.e. it should fill the whole screen.

Solution
As a temporary solution, I patched my /usr/local/lib/python2.7/dist-packages/splinter/driver/webdriver/chrome.py file by adding the following line:

options.add_argument("--start-maximized")

I also reported this idea to the authors of Splinter here.

Categories: python Tags: , , ,

Autoconnect to wifi in my hotel

November 30, 2011 2 comments

Problem
In my hotel there is a fast wifi connection. However, some things trouble me. When I open the browser and I’m not logged in to the wifi, I’m automatically redirected to a page where I must provide my username and password. Unfortunately, the connection is terminated after a certain time so I have to log in again.

Could I use a script for automatically log in to the wifi? Furthermore, if the connection is lost, could it be restarted automatically?

Auto login
This solution is specific to my case but it can be customized to other networks too. Here is my login script:

#!/usr/bin/env python

# wifi.py

from splinter.browser import Browser
from time import sleep

URL = 'https://controller.mobile.lan/103/portal/'
NAME = 'login_name'
PASSWORD = 'login_password'

def main():
    br = Browser('chrome')
    br.visit(URL)
    sleep(3)
    if br.is_text_present('Connection', wait_time=7):
        br.fill('login', NAME)
        br.fill('password', PASSWORD)
        br.find_by_css('#logonForm_connect_button').first.click()

#############################################################################

if __name__ == "__main__":
    main()

I use splinter for browser automation. First, we open the wifi login page (see Figure 1). The login page uses AJAX so we wait some seconds until the page is completely loaded. As an added security, we also verify if the text “Connection” is present. If not, let’s wait some more time. If the page is OK, then fill the fields and click on the form button.


Figure 1: Login page

Figure 2 shows that the connection is established. The red warning message says that the browser must be left opened, otherwise the connection is lost. So from the script we won’t close the browser window. The Python script terminates but the browser window stays there, keeping the wifi connection alive.


Figure 2: Successful login, connection established

Monitoring the network connection
As mentioned above, my connection is terminated after some hours. Here is another script that monitors the network connection. If the network is down, it closes the previously opened browser window and calls the first script (wifi.py) in the background. The first script will establish the connection and the night mass downloading can go on without any problem :)

#!/usr/bin/env python

# monitor_wifi.py

import os
import psutil
from jabbapylib.network import network
from time import sleep
from datetime import datetime
from jabbapylib.process import process

PROCNAME = 'chromedriver'
SLEEP_TIME = 2 * 60

def kill_chromedriver():
#    print '# killing chromedriver'
    for proc in psutil.process_iter():
        if proc.name == PROCNAME:
            proc.kill()

def start_wifi():
    print '# restarting wifi ({now})'.format(now=datetime.now())
    os.chdir('/home/jabba/bin.python')
    process.execute_cmd_in_background("./wifi.py")

def main():
    while True:
        if not network.is_internet_on():
#            print '# network is down'
            kill_chromedriver()
            start_wifi()
        else:
#            print '# network is up'
            pass

#        print '# sleeping...'
        sleep(SLEEP_TIME)

#############################################################################

if __name__ == "__main__":
    main()

Here I use my jabbapylib library.

Usage
For browsing I use Firefox. For logging in I start an instance of Chromium, thus the two things are kept separate. Start wifi.py to create the connection. Then launch monitor_wifi.py. Normally that should be all. I only tested it under Linux.