2012-06-16 25 views
6

Vorrei utilizzare PyQt/QWebview per 1) caricare un URL specifico, 2) immettere informazioni in un modulo, 3) fare clic su pulsanti/collegamenti. Mechanize non funziona perché ho bisogno di un browser vero e proprio.Compilazione di un modulo utilizzando PyQt e QWebview

Ecco il mio codice:

import sys 
from PyQt4.QtCore import * 
from PyQt4.QtGui import * 
from PyQt4.QtWebKit import * 
from PyQt4 import QtCore 

app = QApplication(sys.argv) 
web = QWebView() 
web.load(QUrl("https://www.lendingclub.com/account/gotoLogin.action")) 

def fillForm(): 
    doc = web.page().mainFrame().documentElement() 
    user = doc.findFirst("input[id=master_username]") 
    passwd = doc.findFirst("input[id=master_password]") 

    user.setAttribute("value", "[email protected]") 
    passwd.setAttribute("value", "password") 


    button = doc.findFirst("input[id=master_sign-in-submit]") 
    button.evaluateJavaScript("click()") 

QtCore.QObject.connect(web, QtCore.SIGNAL("loadFinished"), fillForm) 
web.show() 
sys.exit(app.exec_()) 

I pagina viene caricata correttamente, ma non viene immesso alcun input e il modulo non viene presentata. Qualche idea?

risposta

0

Potrebbe essere possibile farlo con Webkit/QWebView ma per quanto riguarda l'utilizzo del selenio: http://code.google.com/p/selenium/? È progettato esattamente per questo tipo di automazione del browser e ha dei begli collegamenti Python.

6

Questo mi ha aiutato per farlo funzionare:

user.setAttribute("value", "[email protected]") 
--> 
user.evaluateJavaScript("this.value = '[email protected]'") 

attributi e proprietà sono cose diverse.

più uno fisso:

click() --> this.click() 
+0

setAttribute opere – Like

0

Per coloro che cercano di fare questo con PyQt5, questo esempio può aiutare come molte cose sono cambiate. Ovviamente il javascript deve essere regolato in base ai contenuti del sito web.

import os 
import sys 
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget 
from PyQt5.QtCore import QUrl, QEventLoop 
from PyQt5.QtWebEngineWidgets import QWebEngineView 

class WebPage(QWebEngineView): 
    def __init__(self): 
     QWebEngineView.__init__(self) 
     self.load(QUrl("https://www.url.com")) 
     self.loadFinished.connect(self._on_load_finished) 

    def _on_load_finished(self): 
     print("Finished Loading") 
     self.page().toHtml(self.Callable) 

    def Callable(self, html_str): 
     self.html = html_str 
     self.page().runJavaScript("document.getElementsByName('loginid')[0].value = '[email protected]'") 
     self.page().runJavaScript("document.getElementsByName('password')[0].value = 'test'") 
     self.page().runJavaScript ("document.getElementById('signin').click()") 

if __name__ == "__main__": 
    app = QApplication(sys.argv) 
    web = WebPage() 
    web.show() 
    sys.exit(app.exec_()) # only need one app, one running event loop