1

Basically I want to go into my Router's settings,
set a checkbutton and than call a function on the page,
all programmatically.

I know all the javascript to do this,
and can do it via the Google Chrome console.

I can syntactically perform it through QWebKit,
but the actual page remains unaffected.

import sys
from PyQt4 import QtCore, QtGui, QtWebKit

class Browser(QtWebKit.QWebView):
    def __init__(self, parent = None):
        super(Browser, self).__init__()
        self.changepage()

    def changepage(self):
        self.load(QtCore.QUrl("http://192.168.0.1/adv_mac_filter.php"))
        x1 = self.page().mainFrame()
        x1.evaluateJavaScript("entry_enable_1.checked = true;")
        x1.evaluateJavaScript("check();")


app = QtGui.QApplication(sys.argv)
x = Browser()
x.show()
sys.exit(app.exec_())

(Yes, this code requires that I am already logged into my routers settings)
I know the JavaScript is being run, as I can test it using Alerts.

However, the routers' html "checked()" function or checkbox aren't ran / changed.
It's like I'm not actually interacting with the page, but a copy.

Am I making a massive rookie mistake here?


Specs:
python 2.7
PyQt4
QWebKit
Windows 7

Anti Earth
  • 4,671
  • 13
  • 52
  • 83
  • Is there a form or something in the page that you have to POST back to the server (that is, the router)? A browser of any kind is *always* working with a local copy of a web page, so something has to communicate back to the server via HTTP, either a form post or some AJAX method. – Pointy Oct 29 '11 at 13:15
  • Probably. I suspected something like this but had no idea how to do it! The Chrome console makes it all seem so easy! – Anti Earth Oct 29 '11 at 13:20
  • Right well it totally depends on how the page works. Is there a button you have to click when you're using the router directly, like a "Save" or something? If you could trigger a click on that element from Python it might do the right thing. – Pointy Oct 29 '11 at 13:25
  • Yeah, it's the button that calls the 'check()' function. That sounds simple enough but I think it would only be harder! :( – Anti Earth Oct 29 '11 at 13:26
  • Ah. Hmm. Well I've never done anything like that; maybe you could check somehow to see whether your application is making HTTP requests back to the router. If it isn't, well, that'd be the basic problem :-) – Pointy Oct 29 '11 at 13:32

1 Answers1

0

You should probably wait for the page to finish loading, by moving the javascript evaluation to a slot connected to the loadFinished() signal.

But as Pointy suggested, you could also try to send POST requests yourself, through QNetworkAccessManager, instead of using a widget.

alexisdm
  • 29,448
  • 6
  • 64
  • 99
  • I have no idea how to go about the latter (the former didn't work). I'm not sure whether I can just post a request to press a button, or I'd have to submit an entire form, I don't know any of it! :D Is there a simpler way to do this outside python? To just use the javascript in a file? – Anti Earth Oct 30 '11 at 01:41
  • You can open chrome developer tools, click on the network tab, and click on the checkbox on the router page. If there is an HTTP request, at that time, it will show in the network tab, and you can click on it to have the request parameters and the router response. – alexisdm Oct 30 '11 at 11:28
  • Changing the state of the checkbox does not cause a HTTP request. I tried clicking the 'save' button (which runs 'check()') which prompts a HTTP request, but I can't click on it because the Network tab is than refreshed. – Anti Earth Oct 30 '11 at 12:49
  • I can see approx 3 new Posts when I click the 'save' button, but then the Network tab is refreshed (becuase I am re-directed to a new page when I hit 'save'). However, if I press save with no changes to the checkboxes made, after the refreshing (which involves redirection to the message: 'no change'), there's a Post with code '200' (which is the same code of the periodic HTTP requests that the router makes to my browser for detecting timeout. they're nothing special in other words (I think)) – Anti Earth Oct 30 '11 at 12:56
  • To keep the previous data in the network tab, you can click on the round icon just above the javascript console (its tooltip says "Preserve Log Upon Navigation"). – alexisdm Oct 30 '11 at 14:11
  • You're a wizard. It posts 3 forms when I click 'save'. Post1 is the state of the first 20 checkboxes (of the 25) on the page. Post2 is the final 5 checkbox states. Post3 is more data off the page (schedules related to checkboxes). – Anti Earth Oct 31 '11 at 00:30