Jump to content
Macro Express Forums

Scripting technique for Web pages...


Recommended Posts

For some webpages, it is easy to figure out exactly which page one is dealing with by checking the window title:

 

Variable Set String %WindowTitle% to topmost window title
If Variable %WindowTitle% Equals "Facebook - Mozilla Firefox"
// This is Facebook
End If
If Variable %WindowTitle% contains "Hotmail"
// This is Hotmail
End If

 

But some sites, like the most recent incarnation of Hotmail, displays the same title for every page, e.g.:

 

Hotmail - myemailaddress@hotmail.com - Windows Live - Mozilla Firefox

 

(In past versions, the name of the current folder appeared in the title: e.g., Hotmail - Inbox...)

 

I am experimenting with a scripting technique to determine the identity of a webpage by checking for unique text in the source code.

 

Here is the script for Firefox:

 

Clipboard Empty
Text Type (Simulate Keystrokes): <ESC> // Cancel menu operation
Text Type (Simulate Keystrokes): <CONTROL>u // Show source 
Wait for Window Title: Source of: // Wait for source window to appear
Text Type (Simulate Keystrokes): <CONTROL>a // Select all
Text Type (Simulate Keystrokes): <CONTROL>c // Copy 
Text Type (Simulate Keystrokes): <ALT><F4> // Close source window
If Clipboard Contains "View New Content"
 // Text is found, so do something
Else
 // Text is NOT found, so do something else
End If

 

Here is the script for Internet Explorer:

 

Clipboard Empty
Text Type (Simulate Keystrokes): <ESC> // Cancel menu operation
Text Type (Simulate Keystrokes): <ALT>v // "&View" menu
Text Type (Simulate Keystrokes): c //  "Sour&ce"
Wait for Window Title: - Original source // Wait for source window to appear
Text Type (Simulate Keystrokes): <CONTROL>a // Select all
Text Type (Simulate Keystrokes): <CONTROL>c // Copy 
Text Type (Simulate Keystrokes): <ALT><F4> // Close source window
If Clipboard Contains "View New Content"
 // Text is found, so do something
Else
 // Text is NOT found, so do something else
End If

 

This technique works nicely if the source contains a unique text string that uniquely identifies a page. But it bothers me that the source window appears briefly. I have tried minimizing the window of source code, hiding it, and moving it off screen. But it is always visible. The effect is rather flashy.

 

Do you know of more elegant ways to save the source to a variable without bringing up a second window?

Link to comment
Share on other sites

Check out http://www.autohotkey.com/docs/commands.htm URLDownLoadtoFile.

 

This might not work when the URL is the same. Try it out and let me know. I have the script that will work no matter the page and you don't have to tolerate that text file opening up to see the html. I even have the script to convert it to text stripping the code.

 

Let me know if you might need it and I will send it to you.

 

Pat

Link to comment
Share on other sites

Where I can I download the HTML file directly to a local temp file with VBScript that I have mentioned here before. This way I can silently process the file for what I need. Very fast, no errors, no timing or browser BS to deal with.

 

 

Link to comment
Share on other sites

Hi Cory,

 

I searched the forums for VBScript, and though I found many mentions, I did not find code samples for extracting the HTML. Where is a good place for a novice VBScripter to start?

 

Related question: I once stumbled upon an on-line tutorial that described, in detail, techniques for interacting with web forms by typing JavaScript on the address line. Now, I can't find it. Can you recommend resources on this subject?

 

Manipulating UIs is a good way when starting macro scripting, but sometimes, it is much better to do the interactions programmatically... but these are not easy skills to acquire!

Link to comment
Share on other sites

Hi Cory,

 

I searched the forums for VBScript, and though I found many mentions, I did not find code samples for extracting the HTML. Where is a good place for a novice VBScripter to start?

 

Related question: I once stumbled upon an on-line tutorial that described, in detail, techniques for interacting with web forms by typing JavaScript on the address line. Now, I can't find it. Can you recommend resources on this subject?

 

Manipulating UIs is a good way when starting macro scripting, but sometimes, it is much better to do the interactions programmatically... but these are not easy skills to acquire!

Link to comment
Share on other sites

Here Alan:

 

http://www.autohotkey.com/forum/topic51020.html&highlight=web+javascript

 

Autothotkey can do it all. You can process anything even if the webpage is invisible!!! You can click, dropdown, delete text, add text, etc. all with IE invisible to the eye.

 

Pat

Hi Pat,

 

Thanks for bringing this to my attention. It looks to be a very good tutorial.

 

Alan

Link to comment
Share on other sites

I searched the forums for VBScript, and though I found many mentions, I did not find code samples for extracting the HTML. Where is a good place for a novice VBScripter to start?

Just google it. I found several. But here's an example of one from my macro. You can effectively ignore all of the bottom part. In this case I'm commanding it with two variables from MEP named %URL% and %Download File%. Download file is the entire path, name, and extension of the file you want it saved to.

' Set your settings
   strFileURL = "%URL%"
   strHDLocation = "%Download File%"

' Fetch the file
   Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")

   objXMLHTTP.open "GET", strFileURL, false
   objXMLHTTP.send()

   If objXMLHTTP.Status = 200 Then
     Set objADOStream = CreateObject("ADODB.Stream")
     objADOStream.Open
     objADOStream.Type = 1 'adTypeBinary

     objADOStream.Write objXMLHTTP.ResponseBody
     objADOStream.Position = 0    'Set the stream position to the start

     Set objFSO = Createobject("Scripting.FileSystemObject")
       If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
     Set objFSO = Nothing

     objADOStream.SaveToFile strHDLocation
     objADOStream.Close
     Set objADOStream = Nothing
   End if

   Set objXMLHTTP = Nothing

Link to comment
Share on other sites

IE also has an API that can be accessed via VBScript which, obviously, doesn't require AutoIT. I've only messed with it a little but i have been doing things like creating a web page, filling out all the fields (mere properties of the object) and clicking the submit button. It's 100% accurate, never suffers from timing issues, and is incredibly fast. Since you are actually talking to IE all of these issues are eliminated.

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...