José Roca Forum
Web Site
News: Attachments are only available to registered users.
Please register using your full, real name.
 
*
Welcome, Guest. Please login or register. July 30, 2010, 12:25:02 AM


Login with username, password and session length


PowerBASIC is a trademark of PowerBASIC, Inc.
This is not an official PowerBASIC site and we are not affiliated with PowerBASIC, Inc.
DISCLAIMER: The software and accompanying documentation are provided "as is" and without warranties as to performance or merchantability or any other warranties whether expressed or implied. Because of the various hardware environments into which the software may be used, no warranty of fitness for a particular purpose is offered. The user must assume the entire risk of using the software. In no case shall any of the contributors to this project be liable for any incidental, special or consequential damages or loss, including, without limitation, lost profits or the inability to use equipment or access data. This is true even if we are advised of the possibility of such damages. We also don't have any obligation of fix eventual bugs or to add new features.
Pages: 1   Go Down
  Print  
Author Topic: Automating Internet Explorer  (Read 1069 times)
0 Members and 1 Guest are viewing this topic.
José Roca
Administrator
Hero Member
*****

Karma: 134
Offline Offline

Gender: Male
Posts: 3653



WWW
« on: September 21, 2008, 10:53:25 PM »

 
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

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

Code:
pIWebBrowser2.Navigate UCODE$("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.

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

Code:
pIHTMLDocument3 = pIHTMLDocument2

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(UCODE$("q"))

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

Code:
pIHTMLElement.setAttribute(UCODE$("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.

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

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 UCODE$("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(UCODE$("q"))
   IF ISNOTHING(pIHTMLElement) THEN EXIT FUNCTION
   ' // Set the value
   pIHTMLElement.setAttribute(UCODE$("value"), "PowerBASIC", 0)
   ' // Get a reference to the submit button
   pIHTMLElement = pIHTMLDocument3.getElementById(UCODE$("btnG"))
   IF ISNOTHING(pIHTMLElement) THEN EXIT FUNCTION
   ' // Click in
   pIHTMLElement.click
  
END FUNCTION
« Last Edit: September 21, 2008, 10:55:06 PM by José Roca » Logged
Pages: 1   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC

IMPRESSUM
Valid XHTML 1.0! Valid CSS! Dilber MC Theme by HarzeM
Page created in 0.107 seconds with 19 queries.