Several methods of QtWebkit operating web pages

One, C++ implementation

[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. }  

This method is effective for pure HTML webpages, but webpages that implement the submission function by javascript may not be effective.

Two, javascript implementation

transfer

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


Three, the combination of the two to achieve

[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. }  


 

Guess you like

Origin blog.csdn.net/jrckkyy/article/details/39001591