[英]Qt - QWebView Problem
我有一個由QWebView小部件組成的PyQt gui腳本。 我正在嘗試發送GET請求,即使用該問題底部的代碼轉到頁面,填寫表格並點擊。
由於我正在處理webview的documentElement(一個QWebElement),我需要將所有DOM操作放在一個單獨的函數中(我將其命名為fillForm)並將loadFinished()信號連接到函數中。 沒有連接信號,文檔將不會加載,我將無法獲得我想要的元素。
我能夠正確提交表格並從網頁上得到適當的回復。
我遇到的問題是,以上情況導致了一種無限循環。 這是因為每次加載新頁面時都會重新加載網頁,因此每次都會在不停止的情況下填充表單。
我想知道是否有某種方法可以確定WebView的頁面是否已完全,非異步地加載,或者暫停了腳本的執行(不凍結gui),直到整個文檔加載完畢。 我無法想出一個令人滿意的解決方案(我的想法包括保持一個全局變量來跟蹤點擊)來解決這個問題。 如果有人可以幫助我以更好的方式解決此問題,我將不勝感激。 謝謝!
這是我正在使用的代碼
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("http://mywebsite.com"))
def fillForm():
doc = web.page().mainFrame().documentElement()
searchform = doc.findFirst("input[type=text]")
searchform.setAttribute("value", "hello")
button = doc.findFirst("input[type=submit]")
button.evaluateJavaScript("click()")
QtCore.QObject.connect(web, QtCore.SIGNAL("loadFinished"), fillForm)
web.show()
sys.exit(app.exec_())
構建“網絡機器人”的同樣問題
我已經使用attesaLoop.exec();
找到了這個解決方案attesaLoop.exec();
暫停所有操作,並在頁面加載文件后webView_loadFinished
恢復執行
在頭文件中聲明
#include <QEventLoop>
QEventLoop attesaLoop;
在功能代碼中
void MainWindow::naviga()
{
//--webView--webPage--webFrame--//--QWebElement--//
indirizzo = "http://www.google.it";
ui->webView->load(QUrl(indirizzo));
attesaLoop.exec();
ui->listWidget->addItem("fine caricamento");
frame = ui->webView->page()->mainFrame();
documento = frame->documentElement();
formInput = documento.findFirst("input[name=q]");
formInput.setAttribute("value","hallo");
bottone = documento.findFirst("input[type=submit]");
bottone.evaluateJavaScript("this.click()");
attesaLoop.exec();
}
void MainWindow::on_webView_loadFinished(bool)
{
//ui->listWidget->addItem(indirizzo);
attesaLoop.exit();
//esegui e;
//e.cerca(documento);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.