IT-Consultant: José Roca (PBWIN 10+/PBCC 6+) (Archive only) > Web Development

Automating Internet Explorer

(1/1)

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.


--- Code: ---pIWebBrowser2 = NEWCOM "InternetExplorer.Application"
pIWebBrowser2.Visible = 1

--- End code ---

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


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

--- End code ---

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


--- Code: ---pIHTMLDocument2 = pIWebBrowser2.Document

--- End code ---

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))


--- Code: ---pIHTMLDocument3 = pIHTMLDocument2

--- End code ---

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


--- Code: ---pIHTMLElement = pIHTMLDocument3.getElementById("q")

--- End code ---

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


--- Code: ---pIHTMLElement.setAttribute("value", "PowerBASIC", 0)

--- End code ---

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


--- Code: ---pIHTMLElement = pIHTMLDocument3.getElementById("btnG")
pIHTMLElement.click

--- End code ---

Full listing


--- Code: ---#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

--- End code ---

Navigation

[0] Message Index

Go to full version