Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Basic Ahk v1.1+ COM Tutorial for Webpages


  • Please log in to reply
138 replies to this topic

Poll: Did this guide help you? (92 member(s) have cast votes)

Did this guide help you?

  1. Ya! COM ftw rawr! (111 votes [86.72%])

    Percentage of vote: 86.72%

  2. No. I am now dumber for having read this... (17 votes [13.28%])

    Percentage of vote: 13.28%

Vote Guests cannot vote
Benny-D
  • Members
  • 1485 posts
  • Last active: Sep 27 2016 11:23 PM
  • Joined: 29 Feb 2008

For DOM (or HTML or DHTML or what have you), how objects work isn't quite as broad as how they work in Excel, but the same principles generally apply. For instance, if you wanted to access a cell in a table, you wouldn't expect to go through the links object to do it. That's why you only access cells through a rows collection, which is accessed through a table object. It's a matter of sorting and limiting how things are accessed.

And just to re-emphasize, Microsoft's HTML and DHTML Reference is an excellent resource to keep around for your own learning

- 1) When you are talking here about objects in HTML here what programming language do you mean? I looked up the references you gave me and it seems that javascript is the language being considered there.

2) When you talk about objects here, is it somehow different from Object-oriented programming or you are still taking about the same thing? I am still at the very beginning of learning OOP in AHK_L;

3) You mentioned HTML here. Actually, I am quite familiar with HTML, but I had no idea that HTML would be somehow related to objects. I thought objects are altogether an attribute of programming in some OOP language, but I have never considered HTML to be a programming language;

4) Thank you for all these references.

Zod
  • Members
  • 214 posts
  • Last active: Jan 26 2012 08:02 PM
  • Joined: 06 Jul 2011
I think the key concept your missing here Benny is that it doesnt matter about the language the objects are the same
the only difference would be syntax accross languages to create an object. in the case of ahk_L vs javascript very little at all will change once you have created the base object.
if in javascript you would go
var somevar = document.getElementsByID("someid").innerText;
then in ahk it would be
somevar := IE.document.getElementsByID("someid").innerText
the current window is assumed from javascript within the page so it isnt created explicitely like you would have to do with ahk
ControlGet,cHwnd, Hwnd,, Internet Explorer_Server1, ahk_class IEFrame
IE  := IE_GetBrowser(cHwnd)

IE_GetBrowser(hWnd) 
   { 
   static IID_IHTMLWindow2   := "{332C4427-26CB-11D0-B483-00C04FD90119}", IID_IWebBrowserApp := "{0002DF05-0000-0000-C000-000000000046}"  
   return, ComObj(9,ComObjQuery(ComObj(9,ComObjQuery(Acc_ObjectFromWindow(hwnd),IID_IHTMLWindow2,IID_IHTMLWindow2),1),IID_IWebBrowserApp,IID_IWebBrowserApp),1)
   } 
is one way to get the topmost browser
Posted Image Stolen from SKAN

Benny-D
  • Members
  • 1485 posts
  • Last active: Sep 27 2016 11:23 PM
  • Joined: 29 Feb 2008

I think the key concept your missing here Benny is that it doesn't matter about the language the objects are the same
the only difference would be syntax across languages to create an object. in the case of ahk_L vs javascript very little at all will change once you have created the base object

- Thank you very much, Zod, for these explanations and for the diagram above. The last piece of code has a lot of mysterious points to me. I think I will firstly try fathoming the matter of objects and object-programming in this thread as I started that one specially for that purpose (I was given a lot of good examples that I haven't fully researched yet. As far as I understand, objects is one of the key new features that make AHK_L different from AHK.

  • Guests
  • Last active:
  • Joined: --
Please help me guys. I'd like to use the InternetExplorer.Application object to download the whole HTML into a variable. But how?

Pwb := ComObjCreate("InternetExplorer.Application")
Pwb.Visible := True
Pwb.Navigate("http://www.google.com")

ok, this works so far. How can I have the whole page HTML in a string variable. I expected...

HTML := Pwb.document.all

will work, but it doensn't, cuz it is an object not a text. And I can not use innertext or innerhtml like this...

HTML := Pwb.document.all.innerhtml

this is simply not working. So, how? Clould you please help?

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009
Pwb.document.documentElement.outerHTML


  • Guests
  • Last active:
  • Joined: --

Pwb.document.documentElement.outerHTML


Thank You!

Jackie Sztuk _Blackholyman
  • Spam Officer
  • 3757 posts
  • Last active: Apr 03 2016 08:47 PM
  • Joined: 28 Feb 2012

Now that weve proven that we can locate a post based on the name and author let's try to navigate to that post. I haven't had any luck using the Click() method to follow the hyperlink so instead were going to use a work around:

URL := IE.LocationURL ; grab the current url
Rows := IE.Document.All.Tags("table")[4].Rows ; table 4 holds all the post information

Loop % Rows.Length
   If InStr(Rows[A_Index].InnerText, "What's on your mind?") and InStr(Rows[A_Index].InnerText, "tomoe_uehara") { ; if post title and author match
      HTML := Rows[A_Index].Cells[1].InnerHTML ; pull the html off the page
      Needle := "viewtopic.php?t=" ; string to search for
      StringGetPos, Pos, HTML, %Needle% ; get starting position of search string
      Pos += 17 ; search 17 characters to the right
      StringMid, Post, HTML, Pos, 5 ; pull the unique post number out of the html
      StringTrimRight, URL, URL, 13 ; cut off forum.php?f=
      URL .= "topic.php?t=" . Post ; set to topic + unique post number
      IE.Navigate(URL) ; navigate to the post
      break
   }
I spent several hours trying to get the Click() method to work for this example but sometimes it's just easier to stick with what you know.


Hey all im a little new to .ahk and programing but im trying to learn all that i can and I've done some trial and error to see if i could get this Click() to work and i found a way and thought i'ed share it here for anyone who'd like to know... :D

Loop % Rows.Length
   If InStr(Rows[A_Index-1].InnerText, "What's on your mind?") and InStr(Rows[A_Index-1].InnerText, "tomoe_uehara") { ; if post title and author match
      Rows[A_Index-1].GetElementsByTagName("A")[0].Click()
   }

The "GetElementsByTagName("A")[0]"'s InnerText is "« What's on your mind? »" you can also use [1] because its the link for page 1 but thats only for multipage threads so no need for that :lol:


At least it works for me :)

Exorbis
  • Members
  • 9 posts
  • Last active: Jan 16 2013 09:47 AM
  • Joined: 21 Feb 2012
Hi Mickers and all others who can help,

Thanks for the great tutorial - now I understand a bit more how the IEGet() function is working and how it could be used. I tried a little bit with this function and was able to "manipulate" some DOM-Objects on Webpages. Now the problem:

I taken an open webpage, set the WB and use the wb.navigate to get to another page. But I have 15 open Tabs in IE and use only 1 to navigate. If the one wich I "manipulate" is not the active one then all happens in background - It is still working fine but I need to search through the 15 Tabs to find the one wich was navigated. Is there any way to set the IE-Tab wich is used for the WB to the active one ( WinActivate only activates when the used Tab is currently the active Tab in this IE Window - but doesn't work if the Tab is "inactive")

Sounds complicated - I know - just as a sample:
I have 3 Tabs (Bing, Google, Autohotkey.com)
Bing is currently the activ Tab
I set the WB to Google and let it Navigate to Ebay
Now the 3 Tabs are Bing, Ebay, Autohotkey.com but as active Tab in Front still Bing is shown but I want to see the Ebay automaticly in front.

I allready mixed up different AHK commands & Javascript like window.focus a.s.o. but it was all ignored by my system... :(

Thanks for answering

crazyace
  • Members
  • 156 posts
  • Last active: Jul 07 2016 06:58 PM
  • Joined: 18 Nov 2010
Is there a way to force the IE pointer to create a new IE window, rather than dealing with the IE settings default of open in current tab/new tab/new window?

Something like this line, but just forcing your own new window preference rather than using the IE defaults:
Pwb := ComObjCreate("InternetExplorer.Application")

I do see that you can use a run switch like this, but then there is no pointer associated with it:
run iexplore.exe -new google.com

Any ideas? Or should I be looking at creating the window with run and then using a function to look for the particular IE window and attach the pointer to it after the fact?

Mickers
  • Members
  • 1239 posts
  • Last active: Sep 25 2015 03:03 PM
  • Joined: 11 Oct 2010
I'm not sure what you mean by "forcing your own new window preference"s. Can you elaborate?
; [email protected]



; create new window

run iexplore.exe -new google.com

Sleep, 5000



; grab new window

pwb := false

for window in ComObjCreate("Shell.Application").Windows

{

	if (window.LocationName == "Google")

	{

		pwb := window

		break

	}

}



; check if window was found

if (!pwb)

{

	MsgBox, Window not found.

	ExitApp

}



; test

pwb.Navigate("autohotkey.com")

Pause
An issues with this script is it will pull the first window named "google" it finds. If you already have an open google window it will connect with that.

nimda
  • Members
  • 4368 posts
  • Last active: Aug 09 2015 02:36 AM
  • Joined: 26 Dec 2010
You can use IEGet() by jethrow... I found a copy of it here: <!-- l --><a class="postlink-local" href="http://www.autohotkey.com/community/viewtopic.php?t=72471">viewtopic.php?t=72471</a><!-- l -->

DZHEZUS
  • Members
  • 11 posts
  • Last active: May 30 2013 03:25 AM
  • Joined: 04 Apr 2012

Tried to control a web page as suggested in the post but got the access denied werror when running following code. Any ideas - note I am logged in as an adminsistrator on the pc.

The error occurs on both the lines starting Pwb.Document

Pwb := ComObjCreate("InternetExplorer.Application") ;create an IE instance
Pwb.Visible := True
Pwb.Navigate("Google.com")
IELoad(Pwb)

Pwb.Document.All.q.Value := "site:autohotkey.com tutorial"
Pwb.Document.All.btnG.Click()
IELoad(Pwb)


i have the same problem, i can't even run the sample code.

could it be 64bit IE? if so, how do i choose which install of IE ComObjCreate creates?

update / edit:

IELoad worked for me when i included its source code (from 1st post) to the bottom of my script.

otherwise, you would change those IEload()s into:

while pwb.busy ; loops until page loads
	continue ; page is loaded, carry on

and here, a function definition

waitload(wb) ;; function accepts an IE instance
{
while wb.busy ; loops until page loads
continue ; page is loaded, carry on
} ;; dzhezus

sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008
You have to turn off "search as you type" on Google before the code will work.

DZHEZUS
  • Members
  • 11 posts
  • Last active: May 30 2013 03:25 AM
  • Joined: 04 Apr 2012

You have to turn off "search as you type" on Google before the code will work.

how do we do that?

Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009

You have to turn off "search as you type" on Google before the code will work.

how do we do that?

I use this as the starting webpage for searches :arrow: http://www.google.com/webhp?complete=0
In the upper-right corner of the page are "Sign in" and a separate gear-shaped icon.
Click the icon to show a menu, select "Search Settings"
Find the text "Google Instant predictions", make your desired selection. I chose "never".
Scroll down the page and click "save".
Close the "Your preferences have been saved" pop-up.

Go ahead with your search.