Jump to content

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

Basic Webpage Controls with JavaScript / COM - Tutorial


  • Please log in to reply
335 replies to this topic
jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009

Could someone please tell me what I am doing wrong here?

Other than not registering so that you can modify your posts rather than reposting :roll:
name := COM_Invoke(pwb, "document.forms[" [color=red]count[/color] "].elements[5].name")

javascript: document.getElementByName('id_tag').click()

Am I doing right? I'm not able to get it to click on it.

No - you are not:

[*:a6iear63]getElement Methods
If you want to get an element(s) based on limited criteria, you can use the following 3 methods:[*:a6iear63]getElementById(id) - returns a reference to the first object with the specified ID
[*:a6iear63]getElementsByName(name) - Returns a collection of objects with the specified name
[*:a6iear63]getElementsByTagName(tagname) - Returns a collection of objects with the specified tagname[/list]



I'm trying to click on a element - but depending on the page reload, it could belong to any one of 6 forms (that has no name).

The best thing to do is find some static parent, child, or sibling element that you can access, then work from there.

Hyun-Su
  • Members
  • 11 posts
  • Last active: Sep 28 2010 12:02 PM
  • Joined: 20 Aug 2010
:D :D :D :D :D :D :D :D
In fact I did not know where I study Com lib. But when I start with this tutorial, I think more easily accessible. Thank you very much.

rhinox202
  • Members
  • 37 posts
  • Last active: Sep 18 2014 07:19 PM
  • Joined: 23 Apr 2009
Hi All,

Back in February I asked about the "Enter" key not working for a website with Flash content. It works on a normal HTML page but, for example, when I use "listen.grooveshark.com" as the URL and press enter after typing in a search word, nothing happens. I was just wondering if anyone has noticed this problem and come up with a solution or if someone has any ideas. Thanks.

Bob

jaysp
  • Members
  • 43 posts
  • Last active: Jan 17 2011 09:21 PM
  • Joined: 08 Jul 2010
Is there a way through COM to click OK on a javascript alert popup?

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009
The alert popup should be a normal window that you can click on with the ControlClick command.

jaysp
  • Members
  • 43 posts
  • Last active: Jan 17 2011 09:21 PM
  • Joined: 08 Jul 2010

The alert popup should be a normal window that you can click on with the ControlClick command.


Thanks! Another quick question, how about clicking a link in a pop up window?

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009
Do you have a specific example? Also, are you using AHK_L or AHK Basic?

jaysp
  • Members
  • 43 posts
  • Last active: Jan 17 2011 09:21 PM
  • Joined: 08 Jul 2010

Do you have a specific example? Also, are you using AHK_L or AHK Basic?


Sure. I'll write an example to make it easier.

test.html
<a href="#" onclick="window.open('test2.html')">Popup (Click First)</a>
<br>
<a href="#" onclick="window.close()">Close (Click Third)</a>

test2.html
<a href="#" onclick="window.close()">Close (Click Second)</a>

At the beginning, test.html would be open. "Popup" would be clicked on test.html, then "Close" on test2.html, then finally "Close" on test.html.

Edit: Both files should be saved to the same directory and I am using AHK Basic.

Thanks again.

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009
What this is going to come down to is this: you will need to access that new pop-up window. There are multiple ways to do this - here's one:

Knowing that the Shell.Application.Windows collection will contain a reference to all the Internet Explorer Objects (& Windows Explorer) in order of creation, we can simply access the last item in the collection:
Com_Init() ; AHK Basic + Com Library

; 1st Window:
	pwb := Com_CreateObject([color=#666666]"InternetExplorer.Application"[/color])
	Com_Invoke(pwb, [color=#666666]"visible="[/color], true)
	Com_Invoke(pwb, [color=#666666]"Navigate"[/color], [color=brown]A_ScriptDir[/color] [color=#666666]"\test.html"[/color])
	[color=#107095]MsgBox[/color], 262144, Allow ActiveX..., Click OK to Click First...
	Com_Invoke(pwb, [color=#666666]"document.links[0].click"[/color])

; 2nd Window:
	oShell := Com_CreateObject([color=#666666]"Shell.Application"[/color])
	oWin := Com_Invoke(oShell, [color=#666666]"Windows"[/color])
	popup_win := Com_Invoke(oWin, [color=#666666]"item"[/color], Com_Invoke(oWin, [color=#666666]"count"[/color])-1)
	Com_Release(oShell), Com_Release(oWin)
	[color=#107095]MsgBox[/color], 262144, Allow ActiveX..., Click OK to Click Second...
	Com_Invoke(popup_win, [color=#666666]"document.links[0].click"[/color])
	Com_Release(popup_win)

; 1st Window:
	[color=#107095]MsgBox[/color], 262144, , Click OK to Click Third...
	Com_Invoke(pwb, [color=#666666]"document.links[1].click"[/color])

Com_Release(pwb), Com_Term()

Here's the same for AutoHotkey_L:
; 1st Window:
	pwb := [color=#107095]ComObjCreate[/color]([color=#666666]"InternetExplorer.Application"[/color])
	pwb.visible := true
	pwb.Navigate([color=brown]A_ScriptDir[/color] [color=#666666]"\test.html"[/color])
	[color=#107095]MsgBox[/color], 262144, Allow ActiveX..., Click OK to Click First...
	pwb.document.links[0].click
; 2nd Window:
	win := [color=#107095]ComObjCreate[/color]([color=#666666]"Shell.Application"[/color]).windows
	popup_win := win.item(win.count-1)
	[color=#107095]MsgBox[/color], 262144, Allow ActiveX..., Click OK to Click Second...
	popup_win.document.links[0].click
; 1st Window:
	[color=#107095]MsgBox[/color], 262144, , Click OK to Click Third...
	pwb.document.links[1].click


jaysp
  • Members
  • 43 posts
  • Last active: Jan 17 2011 09:21 PM
  • Joined: 08 Jul 2010
Slight problem, it doesn't grab the second window, it grabs the first (so it clicks the link on the first page twice). I've tried to change it a bit to get the new window, but I'm stumped.

Below is the original code with a msgbox that shows that popup_win is indeed the first window:

Com_Init() ; AHK Basic + Com Library

; 1st Window:
   pwb := Com_CreateObject("InternetExplorer.Application")
   Com_Invoke(pwb, "visible=", true)
   Com_Invoke(pwb, "Navigate", A_ScriptDir "\test.html")
   MsgBox, 262144, Allow ActiveX..., Click OK to Click First...
   Com_Invoke(pwb, "document.links[0].click")

; 2nd Window:
   oShell := Com_CreateObject("Shell.Application")
   oWin := Com_Invoke(oShell, "Windows")
   popup_win := Com_Invoke(oWin, "item", Com_Invoke(oWin, "count")-1)
   Com_Release(oShell), Com_Release(oWin)
   MsgBox, 262144, Allow ActiveX..., Click OK to Click Second...
   curWindow:=COM_Invoke(popup_win, "LocationName") 
   msgbox, %curWindow%
   Com_Invoke(popup_win, "document.links[0].click")
   Com_Release(popup_win)

; 1st Window:
   MsgBox, 262144, , Click OK to Click Third...
   Com_Invoke(pwb, "document.links[1].click")

Com_Release(pwb), Com_Term()

Thanks.

Edit: btw, it is the same for AHK_L. Same issue.

Edit2: If fixed, will this work even if the window is not visible? I ultimately want to be able to navigate a site, scrape for the information I need on the page, and return it to my script transparently.

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009
Try putting a MsgBox in there before operating on the second window - to ensure the window is created. If that still doesn't work, you may have to access the window in another manner (perhaps IEGet function?):
MsgBox, Wait for 2nd window to be created...
; 2nd Window:

... will this work even if the window is not visible?

It should.

jaysp
  • Members
  • 43 posts
  • Last active: Jan 17 2011 09:21 PM
  • Joined: 08 Jul 2010

Try putting a MsgBox in there before operating on the second window - to ensure the window is created. If that still doesn't work, you may have to access the window in another manner (perhaps IEGet function?):

MsgBox, Wait for 2nd window to be created...
; 2nd Window:

... will this work even if the window is not visible?

It should.


Nope. Doesn't work.

I am not sure what IEGet is.

Shouldn't I also be able to get the window by using a Process, Exist, 'Name of the window' and pass this to COM?

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009
When I say access the window, what I mean is get a pointer the either the Window Object or the WebBrowser Object, which will allow you to utilize the COM interface. This won't be accomplished with the standard AHK commands.

I am not sure what IEGet is.

Check out the FAQs for this tutorial.

octalmage as guest
  • Guests
  • Last active:
  • Joined: --
Is there anyway go get the history of the browser control? Also is there some "can go back", and "cangoforward" commands?

Thanks,
Jason

OctalMage as guest
  • Guests
  • Last active:
  • Joined: --
Also is there anyway to set the control up to use proxies?

Thanks again,
Jason