QtWebkit操作网页的几种方法

一、C++实现

[cpp]  view plain copy
  1. QWebFrame* frame = webView()->page()->currentFrame();  
  2. if (frame!=NULL){  
  3.     QWebElementCollection collection1 = frame->findAllElements(“input[name=submit]”);  
  4.     foreach (QWebElement element, collection1){  
  5.   
  6.         QPoint poss(element.geometry().center());  
  7.         QPoint pos = this->topLevelWidget()->mapToGlobal(QPoint(0,0));  
  8.         QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);  
  9.         QApplication::sendEvent(webView->page(), &event0);  
  10.         QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);  
  11.         QApplication::sendEvent(webView->page(), &event1);  
  12.     }  
  13. }  

这种方法对于纯HTML的网页有效,但是javascript实现提交功能的网页不一定有效。

二、javascript实现

调用

[cpp]  view plain copy
  1. evaluateJavaScript(code);  


三、两者结合实现

[cpp]  view plain copy
  1. QWebFrame* frame = webView()->page()->currentFrame();  
  2. if (frame!=NULL) {  
  3.     QWebElementCollection collection1 = frame->findAllElements(“input[name=submit]”);  
  4.     foreach (QWebElement element, collection1){  
  5.         element.evaluateJavaScript("this.click();");  
  6.     }  
  7. }  


 

猜你喜欢

转载自blog.csdn.net/jrckkyy/article/details/39001591