Automating Internet Explorer

Started by José Roca, August 29, 2011, 12:24:36 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca

 
The followig example navigates to Google and searches for PowerBASIC".

In this example, "q" is the name of the input control and "btnG" is the name of the submit button on the Google page.

The first step is to create an instance of Internet Explorer and make it visible.


pIWebBrowser2 = NEWCOM "InternetExplorer.Application"
pIWebBrowser2.Visible = 1


The second step is to navigate to the Google page and wait until it is ready.


pIWebBrowser2.Navigate "http://www.google.com/"
WHILE (pIWebBrowser2.ReadyState <> %READYSTATE_COMPLETE)
   apiSleep 3
WEND


The third step is to retrieve a reference to the automation object of the active document.


pIHTMLDocument2 = pIWebBrowser2.Document


The fourth step is to retrieve a reference to the Google's input box. This can be achieved using the getElementById method of the IHTMLDocument3 interface.

With PowerBASIC, the following assignment performs a call to IUnknown.QueryInterface. It is equivalent to pIHTMLDocument2.QueryInterface (IID_IHTMLDocument3, BYVAL VARPTR(pIHTMLDocument3))


pIHTMLDocument3 = pIHTMLDocument2


Now, we can call getElementById to retrieve a reference to the Google's input box, that uses "q" as the identifier.


pIHTMLElement = pIHTMLDocument3.getElementById("q")


The next step if to set the value attribute with the string to search.


pIHTMLElement.setAttribute("value", "PowerBASIC", 0)


Finally, we retrieve a reference to the submit button, that uses "btnG" as the identifier, and click it calling the click method.


pIHTMLElement = pIHTMLDocument3.getElementById("btnG")
pIHTMLElement.click


Full listing


#COMPILE EXE
#DIM ALL
#INCLUDE "exdisp.inc"
#INCLUDE "mshtml.inc"

FUNCTION PBMAIN () AS LONG

   LOCAL pIWebBrowser2   AS IWebBrowser2     ' // Reference to the IWebBrowser2 interface
   LOCAL pIHTMLDocument2 AS IHTMLDocument2   ' // Reference to the IHTMLDocument2 interface
   LOCAL pIHTMLDocument3 AS IHTMLDocument3   ' // Reference to the IHTMLDocument3 interface
   LOCAL pIHTMLElement   AS IHTMLElement     ' // Reference to the IHTMLElement interface
   
   ' // Create a new instance of Internet Explorer
   pIWebBrowser2 = NEWCOM "InternetExplorer.Application"
   IF ISNOTHING(pIWebBrowser2) THEN EXIT FUNCTION

   ' // Make it visible
   pIWebBrowser2.Visible = 1

   ' // Navigate to Google
   pIWebBrowser2.Navigate "http://www.google.com/"

   ' // Wait until the page is ready
   WHILE (pIWebBrowser2.ReadyState <> %READYSTATE_COMPLETE)
      apiSleep 3
   WEND

   ' // Get a reference to the IHTMLDocument2 interface
   pIHTMLDocument2 = pIWebBrowser2.Document
   IF ISNOTHING(pIHTMLDocument2) THEN EXIT FUNCTION

   ' // Get a reference to the IHTMLDocument3 interface
   pIHTMLDocument3 = pIHTMLDocument2
   IF ISNOTHING(pIHTMLDocument3) THEN EXIT FUNCTION

   ' // Get a reference to the input box
   pIHTMLElement = pIHTMLDocument3.getElementById("q")
   IF ISNOTHING(pIHTMLElement) THEN EXIT FUNCTION

   ' // Set the value
   pIHTMLElement.setAttribute("value", "PowerBASIC", 0)

   ' // Get a reference to the submit button
   pIHTMLElement = pIHTMLDocument3.getElementById("btnG")
   IF ISNOTHING(pIHTMLElement) THEN EXIT FUNCTION

   ' // Click in
   pIHTMLElement.click
   
END FUNCTION