• Welcome to Theos PowerBasic Museum 2017.

News:

Attachments are only available to registered users.
Please register using your full, real name.

Main Menu

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


John Thompson

Hi,

I'm using PB 10.02.0099.  The code works perfectly on one page, but I want to continue after submitting the first page, and am not sure where I'm going wrong...


#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

    'Login
    pIWebBrowser2.Navigate "mysite"
    WHILE (pIWebBrowser2.ReadyState <> %READYSTATE_COMPLETE)
        apiSleep 3
    WEND
    pIHTMLDocument2 = pIWebBrowser2.Document
    IF ISNOTHING(pIHTMLDocument2) THEN EXIT FUNCTION
    pIHTMLDocument3 = pIHTMLDocument2
    IF ISNOTHING(pIHTMLDocument3) THEN EXIT FUNCTION
    'Login
    pIHTMLElement = pIHTMLDocument3.getElementById("username")
    IF ISNOTHING(pIHTMLElement) THEN EXIT FUNCTION
    pIHTMLElement.setAttribute("value", "myusername", 0)
    'Password
    pIHTMLElement = pIHTMLDocument3.getElementById("password")
    IF ISNOTHING(pIHTMLElement) THEN EXIT FUNCTION
    pIHTMLElement.setAttribute("value", "mypassword", 0)
    'Click button
    pIHTMLElement = pIHTMLDocument3.getElementById("idSubmit")
    IF ISNOTHING(pIHTMLElement) THEN EXIT FUNCTION
    pIHTMLElement.click

'Doesn't do anything (below)

    'Location
    WHILE (pIWebBrowser2.ReadyState <> %READYSTATE_COMPLETE)
        apiSleep 3
    WEND
    pIHTMLDocument2 = pIWebBrowser2.Document
    IF ISNOTHING(pIHTMLDocument2) THEN EXIT FUNCTION
    pIHTMLDocument3 = pIHTMLDocument2
    IF ISNOTHING(pIHTMLDocument3) THEN EXIT FUNCTION
    'Location
    pIHTMLElement = pIHTMLDocument3.getElementById("idLocation")
    IF ISNOTHING(pIHTMLElement) THEN EXIT FUNCTION
    pIHTMLElement.setAttribute("value", "123", 0)
    'Click
    pIHTMLElement = pIHTMLDocument3.getElementById("idSubmit")
    IF ISNOTHING(pIHTMLElement) THEN EXIT FUNCTION
    pIHTMLElement.click

END FUNCTION


Everything works fine for the first submission, but on the second half nothing happens.  If I run in the debugger and step through, everything works OK.  I thought I remembered reading a post about this situation, and what caused it, but I can't find it.

Can anyone help point me in the right direction?

Thanks!

-John