Question

I'm trying to submit a form using PyQt. I started out using it via Spynner but I've deconstructed that in my repl to just the raw PyQT calls themselves to try to debug the issue.

>>> obr.webframe.findAllElements('.qust input').toList()
[<PyQt4.QtWebKit.QWebElement object at 0xa343f7c>, <PyQt4.QtWebKit.QWebElement object at 0xa343fb4>]
>>> 
>>> elem = obr.webframe.findAllElements('.qust input').toList()[1]
>>> elem.setFocus()
>>> elem.evaluateJavaScript("this.click()") 

And when that didn't work, I pulled this snippet from the method in Spynner that invokes a click event, which also didn't work:

>>> jscode =
"var e = document.createEvent('MouseEvents');e.initEvent( 'click', true, true );this.dispatchEvent(e);"
>>> 
>>> elem.evaluateJavaScript(jscode)

And then finally:

>>> obr.webframe.findFirstElement('form')
<PyQt4.QtWebKit.QWebElement object at 0xa34387c>
>>> obr.webframe.findFirstElement('form').evaluateJavaScript('this.submit();')
<PyQt4.QtCore.QVariant object at 0xa343f44>
>>> 

In all cases, it instantly just returns. No form post is made. The form is normal:

>>> obr.webframe.findFirstElement('form').attribute('action')
PyQt4.QtCore.QString(u'SubmitPage.aspx?g=d7a717c38&ust=72a0fc7b&nm=01f00&se=53')
>>> obr.webframe.findFirstElement('form').attribute('method')
PyQt4.QtCore.QString(u'post')

If anybody has any thoughts or suggestions, I'd be SUPER appreciative. As you can see, I've been just grasping at straws here. Not my preferred debugging style...

Was it helpful?

Solution

You should go with the click solution, as this will ensure that if the page has some javascript code executed when the submit button is clicked, it will be called too. It can change or even add additional fields to the post data. Generally the code you used is correct, try to start the javascript click event with this snippet:

elem = obr.webframe.findAllElements('.qust input').toList()[1]
js_click = """
           var evt = document.createEvent("MouseEvents");
           evt.initMouseEvent("click", true, true, window, 1, 1, 1, 1, 1, false, false, false, false, 0, this);
           this.dispatchEvent(evt);
           """
elem.evaluateJavaScript(js_click)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top