一、C++实现
QWebFrame* frame = webView()->page()->currentFrame(); if (frame!=NULL){ QWebElementCollection collection1 = frame->findAllElements(“input[name=submit]”); foreach (QWebElement element, collection1){ QPoint poss(element.geometry().center()); QPoint pos = this->topLevelWidget()->mapToGlobal(QPoint(0,0)); QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); QApplication::sendEvent(webView->page(), &event0); QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); QApplication::sendEvent(webView->page(), &event1); } }
这种方法对于纯HTML的网页有效,但是javascript实现提交功能的网页不一定有效。
二、javascript实现
调用
evaluateJavaScript(code);
三、两者结合实现
QWebFrame* frame = webView()->page()->currentFrame(); if (frame!=NULL) { QWebElementCollection collection1 = frame->findAllElements(“input[name=submit]”); foreach (QWebElement element, collection1){ element.evaluateJavaScript("this.click();"); } }