WebBrowser to get Jump page document interface source code

Source: Internet
Author: User

The origin of the problem is this, today to help a netizen solve the problem, said from the VC station download a source code, the function of the program is mainly in the dialog box placed a WebBrowser control, the program starts by default call this code:

M_web. Navigate (_t ("https://www.baidu.com/s?wd=400 phone"), NULL, NULL, NULL, NULL);


Open this URL:
Https://www.baidu.com/s?wd=400 telephone, such as:


After clicking the "Get Test" button, perform the following function:

1 voidCcctrydlg::onbnclickedbutton1 ()2 {3CComQIPtr <ihtmldocument2, &IID_IHTMLDocument2> spDoc1 =m_web.get_document ();4IHTMLDocument3 *PDOC3 =NULL;5HRESULT hr = Spdoc1->queryinterface (Iid_ihtmldocument3, (void* *) &pDoc3);6         if(!PDOC3 && FAILED (HR))return;7CComPtr <IHTMLElement>puserelement;8CComBSTR Idname (Ct2ole (_t ("kw")));//get edit box element ID9hr = Pdoc3->getelementbyid (Idname, &puserelement);Ten         if(FAILED (HR) | |! Puserelement)return; OnePuserelement->put_innertext (CComBSTR ("New Page"));//Write String ACComPtr <IHTMLElement>pbtnelement; -CComBSTR Idbtnname (Ct2ole (_t ("su")));//gets the element ID of the form button -hr = Pdoc3->getelementbyid (Idbtnname, &pbtnelement); the         if(FAILED (hr) | |!pbtnelement)return; -Pbtnelement->click ();//Simulation Click on the Baidu button to search -         //---------------------------------------------------------------------------------- -         //get all links after clicking the Baidu button +Sleep ( the);//the newly opened Web page is finished loading -         //again, but the link is still in the original 400 phone, not in the new page.  +CComQIPtr <ihtmldocument2, &IID_IHTMLDocument2> spDoc2 =m_web.get_document (); A getalllinks (SPDOC2); at}


Simple explanation: is to get Baidu search box interface, and then enter the keyword inside: "New page", then get the "Baidu" button interface, call this sentence Pbtnelement->click (); Trigger a Click event, plainly, is to search the current page for the keyword "new page".
, call Sleep (5000), wait for a new page to load, call M_web.get_document again (), get the document interface for the current Web page, and then call Getalllinks (SPDOC2); The function parses the URL link of all search results for the current page, which is displayed in the list below the software, such as
 

However, the problem comes, we look at the above diagram, the list of URLs are shown in the previous URL search "400 phone" keyword results, Not after the search for the keyword "new page" URL link, this is what happened? It's inconsistent with the results we asked for.
I also deliberately called Sleep (5000), waiting for 5 seconds, how is the result or not?

So ... Run around, Google search a lot of, or did not find the results, to Microsoft Official MSDN also did not find anything fishy, in the end is how the matter, is ready to give up, suddenly inspiration came,

So according to this idea, I commented out the following few words in the Get Test button response function:

Sleep ( ccomqiptr <ihtmldocument2, &IID_IHTMLDocument2> spDoc2 after loading the newly opened Web page = m_web.get_document (); Getalllinks (SPDOC2);


That is, do not let him Sleep, nor let him in the current response function of the button to get all the new page of the child links, directly triggered "Baidu" button click event after the end.
Next, I add a button to the interface, named "Re-Test", in the response function of the button to add the following code:

CComQIPtr <ihtmldocument2, &IID_IHTMLDocument2> spDoc2 = m_web.get_document (); Getalllinks (SPDOC2);

That is, in the "Re-test" button response function to get all the child links in the new page to see if the success! The result? Haha, of course, is successful, such as:


See, this time the list shows the page is already a new page sub-link ...

Well, the article is written here, I hope other users encounter the same problem to see this article, less detours!

Related engineering source download see: http://www.cctry.com/thread-254314-1-1.html

WebBrowser to get Jump page document interface source code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.