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
sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008
Have you navigated to the page yet?

[color=red]url:="http://www.thatwebsiteyourlywant2get2.com"[/color]
COM_Init() 
pwb := COM_CreateObject("InternetExplorer.Application") 
COM_Invoke(pwb , "Visible=", "True") ;"False" ;"True" ; 
[color=red]COM_Invoke(pwb, "Navigate",url)
Loop
  if (COM_Invoke(pwb,"readyState") = 4) ; wait for page to load
    break[/color]
text := COM_Invoke(pwb, "document.documentElement.outerHTML") ; should be correct to get the source 
msgbox %text%


randallf
  • Members
  • 711 posts
  • Last active: Jan 07 2014 11:45 PM
  • Joined: 06 Jul 2009

Have you navigated to the page yet?

[color=red]url:="http://www.thatwebsiteyourlywant2get2.com"[/color]
COM_Init() 
pwb := COM_CreateObject("InternetExplorer.Application") 
COM_Invoke(pwb , "Visible=", "True") ;"False" ;"True" ; 
[color=red]COM_Invoke(pwb, "Navigate",url)
Loop
  if (COM_Invoke(pwb,"readyState") = 4) ; wait for page to load
    break[/color]
text := COM_Invoke(pwb, "document.documentElement.outerHTML") ; should be correct to get the source 
msgbox %text%


This is awesome, and exactly the example I'm looking for (thank you!) but I still receive an error.

I am using AHK_L and the COM (not _L) standard library at the moment. I'm thinking this might be the problem.

Do I have to use the latest official AHK with this?
Can I use AHK_L and the COM_L library?

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009

I am using AHK_L and the COM (not _L) standard library at the moment.

Don't do that :(

Can I use AHK_L and the COM_L library?

Try this:
url:="http://www.thatwebsiteyourlywant2get2.com" 
pwb := COM_CreateObject("InternetExplorer.Application") 
pwb.Visible := True
pwb.Navigate(url) 
Loop 
  if (pwb.readyState = 4) ; wait for page to load 
    break 
text := pwb.document.documentElement.outerHTML ; should be correct to get the source 
msgbox %text%


Gundeck
  • Members
  • 5 posts
  • Last active: Feb 04 2010 10:09 PM
  • Joined: 26 Jan 2010
[*]Dropdown Box Selection - selectedIndex

<SELECT class=post name=sort_by><OPTION selected value=0>Post Time</OPTION><OPTION value=1>Post Subject</OPTION><OPTION value=2>Topic Title</OPTION><OPTION value=3>Author</OPTION><OPTION value=4>Forum</OPTION></SELECT>

This is the HTML for the Sort By Dropdown. The following will set the Dropdown to Author:
javascript: document.all.sort_by.selectedIndex = 3; void 0   ; Note - you could use [color=blue]value = 3[/color]
[color=green]COM_Invoke(pwb, "document.all.sort_by.selectedIndex", 3)[/color]

I'm having troube getting the example to work with a drop down.
When I use IE HTML Element SPY I get a code of:
<SELECT style="WIDTH: 100px" id=selectedStatus onchange=javascript:changeMailingStatus(this.value,20); name=test> <OPTION selected value=0>Select Status</OPTION> <OPTION value=5>Archive</OPTION></SELECT>

There are multiple drop dows on the page. The next one would be:
<SELECT style="WIDTH: 100px" id=selectedStatus onchange=javascript:changeMailingStatus(this.value,21); name=test> <OPTION selected value=0>Select Status</OPTION> <OPTION value=5>Archive</OPTION></SELECT>

Any insight on how I would get AHK to make the appropriate selection to 5>Archive?

Thank You for the support.

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009
It should just be:
javascript: document.all.selectedStatus.selectedIndex = 5; void 0   ; Note - you could use [color=blue]value = 5[/color] 
[color=green]COM_Invoke(pwb, "document.all.selectedStatus.selectedIndex", 5)[/color]

Now, this drop down also has an "onchange" event

:<SELECT style="WIDTH: 100px" id=selectedStatus onchange=javascript:changeMailingStatus(this.value,20); name=test> <OPTION selected value=0>Select Status</OPTION> <OPTION value=5>Archive</OPTION></SELECT>

You should be able to invoke this using the fireEvent method:
javascript: document.all.selectedStatus.selectedIndex.fireEvent('onchange')
[color=green]COM_Invoke(pwb, "document.all.selectedStatus.selectedIndex.fireEvent", "onchange")[/color]
NOTE - untested

Gundeck
  • Members
  • 5 posts
  • Last active: Feb 04 2010 10:09 PM
  • Joined: 26 Jan 2010

It should just be:

javascript: document.all.selectedStatus.selectedIndex = 5; void 0   ; Note - you could use [color=blue]value = 5[/color] 
[color=green]COM_Invoke(pwb, "document.all.selectedStatus.selectedIndex", 5)[/color]

Now, this drop down also has an "onchange" event

:<SELECT style="WIDTH: 100px" id=selectedStatus onchange=javascript:changeMailingStatus(this.value,20); name=test> <OPTION selected value=0>Select Status</OPTION> <OPTION value=5>Archive</OPTION></SELECT>

You should be able to invoke this using the fireEvent method:
javascript: document.all.selectedStatus.selectedIndex.fireEvent('onchange')
[color=green]COM_Invoke(pwb, "document.all.selectedStatus.selectedIndex.fireEvent", "onchange")[/color]
NOTE - untested


How do I diferentiate bewteen the different dropdowns?
There are multiple per page, the only thing I can see that could possably be an identifier is "(this.value,20)" which inciments +1 for the next dropdown available.
I assume I somehow need to include that into the scripting?

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009

How do I diferentiate bewteen the different dropdowns?

By "dropdowns", I'm assuming you mean different dropdown boxes, and not the different options in the dropdown. Each dropdown should have a unique id

<SELECT style="WIDTH: 100px" id=selectedStatus onchange=javascript:changeMailingStatus(this.value,20); name=test> <OPTION selected value=0>Select Status</OPTION> <OPTION value=5>Archive</OPTION></SELECT>

You're examples don't seem to follow this, which I haven't seen before. You could also get a collection of all the SELECT (dropdown) elements on the page:
javascript: select=document.all.tags('SELECT'); alert('There are ' + select.length + ' dropdowns on the page.')
[color=green]select := COM_Invoke(pwb, "document.all.tags", "SELECT")
MsgBox, % "There are " COM_Invoke(select, "length") " dropdowns on the page."[/color]
*Remember, the variable "select" will now be an object, and needs to be released

You could now access the dropdown by which item it is in the collection (collection starts at 0):
select[0]
[color=green]COM_Invoke(select, "item", 0)[/color]


  • Guests
  • Last active:
  • Joined: --

I am using AHK_L and the COM (not _L) standard library at the moment.

Don't do that :(

Can I use AHK_L and the COM_L library?

Try this:
url:="http://www.thatwebsiteyourlywant2get2.com" 
pwb := COM_CreateObject("InternetExplorer.Application") 
pwb.Visible := True
pwb.Navigate(url) 
Loop 
  if (pwb.readyState = 4) ; wait for page to load 
    break 
text := pwb.document.documentElement.outerHTML ; should be correct to get the source 
msgbox %text%


This is really cool, I finally got it working - I think I had the wrong version of COM_L, I guess 2 versions come in the one zip??? - anyway, this works really great but is there a way to make it do it in the background, without opening a window?

Thanks again!!! :)

randallf
  • Members
  • 711 posts
  • Last active: Jan 07 2014 11:45 PM
  • Joined: 06 Jul 2009

This is really cool, I finally got it working - I think I had the wrong version of COM_L, I guess 2 versions come in the one zip??? - anyway, this works really great but is there a way to make it do it in the background, without opening a window?

Thanks again!!! :)


I hardly deserve to call myself a programmer,

pwb.Visible := False

when I miss stuff like this all the time.

:D TY!!

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009
url:="http://www.thatwebsiteyourlywant2get2.com" 

pwb := COM_CreateObject("InternetExplorer.Application") 

[color=red]pwb.Visible := True [/color] [color=green]; <--- leave this line out & the window will be Invisible ;)

[/color]pwb.Navigate(url) 

Loop 

  if (pwb.readyState = 4)

    break 

text := pwb.document.documentElement.outerHTML

msgbox %text%

[color=red]pwb.Quit()[/color] ; <--- this will close the Invisible IE window


Gundeck
  • Members
  • 5 posts
  • Last active: Feb 04 2010 10:09 PM
  • Joined: 26 Jan 2010
javascript: document.getElementsByTagName('input')[11].click()
COM_Invoke(pwb, "javascript: document.getElementsByTagName[input].item[11].click")

This is from the Tut.
What I'm trying to do is just a simple login into Facebook.
I got it entering in the User and Password using

COM_Invoke(userName:=COM_Invoke(all:=COM_Invoke(doc:=COM_Invoke(pwb,"Document"),"All"),"Item","Email"),"value",gUser)

COM_Invoke(password:=COM_Invoke(all,"Item","pass_placeholder"),"value",gPass)

from the COM tut, but I could not get this to work:
COM_Invoke(submit:=COM_Invoke(all,"Item","submit"),"click").
Mostly because the button does not have a name field os the other had. When I use various spy apps it comes up with:
,input value="Login" type="submit" class="UIButton_Text.

I can use:
javascript: document.getElementsByTagName('input')[6].click()
to fire it off, but it COM equivelent:
COM_Invoke(pwb, "javascript: document.getElementsByTagName[input].item[11].click")

Pops up errors.

I'm pretty nub in this whole thing, I have been following posts and have review almost every topic that involves "Facebook". I really don't want to send click or {TAB} {ENTER} to fire this button off. Am I missing something obvious?

Thanks all.

sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008

Am I missing something obvious?


Not entirely. Even though the input button does not have a name or ID, your information does show that it has a value:

input [color=red]value="Login"[/color] type="submit" class="UIButton_Text.

So what you need to do is look through input element collection, find the one with a value "Login" and click it:

COM_Invoke(pwb,"document.all.item[email].value",gUser)
COM_Invoke(pwb,"document.all.item[pass_placeholder].value",gPass)
Loop % COM_Invoke(pwb,"document.all.tags[input].length") { ; loop runs equal to the number of input elements on the page
  if (COM_Invoke(pwb,"document.all.tags[input].item[" A_Index-1 "].value") = "Login") { ; if current input element has value of 'Login'
    COM_Invoke(pwb,"document.all.tags[input].item[" A_Index-1 "].click") ; click it
    break ; break the loop
  } ; else the loop continues
}


jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009

COM_Invoke(pwb, "javascript: document.getElementsByTagName[input].item[11].click")

Thanks for pointing this out. Remove the red. I have updated the tutorial.

rhinox202
  • Members
  • 37 posts
  • Last active: Sep 18 2014 07:19 PM
  • Joined: 23 Apr 2009
Is there a way to add !important to the following code?
COM_Invoke(pwb, "document.getElementById[box].style.display", "none")
I have tried adding " !important" after none, but that doesn't work. Doing so results in the following:

COM Error Notification
Function Name: "display"
ERROR: Not Implemented
(0x80004001)
ERROR2: The parameter is incorrect
(0x80070057)

I do not receive an error like this when adding !important to a margin style, so I am guessing that it may not be possible on the display style. If anyone can help I would appreciate it. Thanks.

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
what on earth makes you think you should
Never lose.
WIN or LEARN.