using QWebElement with python

Inspired by http://drupal4hu.com/node/266 in started to write a small python script which fills out the cisco "network authentication system" on my home.

Therefore the script has to fill out a form and click a Login button. My connection drops every 8 hours so i thought it would be cool to automatically login every 8-N hours so i don't have to do it automatically any more.

So here is a small script

Inspired from http://drupal4hu.com/node/266

These lines will get us the modules we need.

from PyQt4.QtCore import QUrl, SIGNAL
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QTimer
from PyQt4.QtCore import QObject
from PyQt4.QtCore import QSettings
from PyQt4.QtWebKit import QWebPage, QWebView

Importet to get script arguments.

import getopt
import sys

class Scrape(QApplication):
def init(self):
# Apparently there are a number of versions of this init and PyQT
# figures out which you want based on the number of arguments. So pass
# in one argument but we do not need anything really, so None.
super(Scrape, self).init(None)

# Setup the settings
self.settings = QSettings('netaccess')
self.loadSettings()

# Load the command line arguments
self.loadArguments()

# Create a QWebView instance and store it.
self.webView = QWebView()
# Connect our loadFinished method to the loadFinished signal of this new
# QWebView.
if self.simultate == 0:
self.webView.loadFinished.connect(self.loadFinished)
else:
print "debugging enabled"

# Get the script arguments/options
def loadArguments(self):
self.simultate = 0
# I would like to know what this arguments really are
opts, extraparams = getopt.getopt(sys.argv[1:], 's:u:p')
for o,p in opts:
if o in ['-s','--simulate']:
self.simultate = 1
if o in ['-u', '--username']:
self.username = p
self.writeSettings()
if o in ['-p', '--password']:
self.password = p
self.writeSettings()

def load(self, url):
# In the init we stored a QWebView instance into self.webView so
# we can load a url into it. It needs a QUrl instance though.
self.webView.load(QUrl(url))

# Write down some basic settings.
def writeSettings(self):
self.settings.setValue('username', self.username)
self.settings.setValue('password', self.password)

# Load settings with default values
def loadSettings(self):
self.username = self.settings.value('username', 'default').toString()
self.password = self.settings.value('password', 'default').toString()

def loadFinished(self):
self.webView.loadFinished.disconnect(self.loadFinished)

# Set up a qtimer to execute inputData every second
self.ctimer = QTimer()
minutes = 30
seconds = 60 * minutes
milliseconds = seconds * 60
self.ctimer.start(1000)
self.ctimer.setInterval(1000)

#QObject.connect(self.ctimer, SIGNAL("timeout()"), self.inputData)
self.ctimer.timeout.connect(self.inputData)

# We are inside a QT application and need to terminate that properly.
#self.exit()

def inputData(self):
# We landed here because the load is finished. Now, load the root document
# element. It'll be a QWebElement instance. QWebElement is a QT4.6
# addition and it allows easier DOM interaction.
documentElement = self.webView.page().currentFrame().documentElement()

# Find login and username/password
field_login = documentElement.findFirst('input[name="Login"]')
field_username = documentElement.findFirst('input[name="username"]')
field_password = documentElement.findFirst('input[name="password"]')

# Set the username and password.
field_username.setAttribute('value', self.username)
field_password.setAttribute('value', self.password)

self.webView.loadFinished.connect(self.LoginResult)

field_login.evaluateJavaScript('this.click()')

def LoginResult(self):
print "Login done"
# TODO Figure out what do to.

Instantiate our class.

myScrape = Scrape()

Load the Netaccess page.

myScrape.load('http://10.199.240.1/netaccess/loginuser.html')

Start the QT event loop.

myScrape.exec_()

Blog:

Add new comment