Click Elements by ID Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
rockinrick28
Posts: 10
Joined: 01 Dec 2017, 23:34

Click Elements by ID

01 Dec 2017, 23:50

Hey guys, I’m brand new to this (started today) and I have a problem I can’t find a solution for.

I just need to know if there is a command that will let me locate an element on a webpage in IE, and button in this case, by its ID and click on it.

Is there a command someone can give me and explain to me where I can just plug in the ID of “61” and get the result I want?
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Click Elements by ID

02 Dec 2017, 06:18

What browser you're using? Best chance if it's IE bc AHK offers its ComObj-command set to trigger things. Good luck :)
rockinrick28
Posts: 10
Joined: 01 Dec 2017, 23:34

Re: Click Elements by ID

02 Dec 2017, 08:28

Yes I’m using IE
rockinrick28
Posts: 10
Joined: 01 Dec 2017, 23:34

Re: Click Elements by ID

02 Dec 2017, 08:42

Like I said in the original post, I'm using IE. I know that ComObj can do what im looking for, but i just need that one command right now and dont have time to learn the whole thing yet.
HIAC
Posts: 111
Joined: 11 Oct 2017, 17:59
Location: Republic of Serbia

Re: Click Elements by ID

02 Dec 2017, 10:34

Hope this helps :)

Code: Select all

pwb := ComObjCreate("InternetExplorer.Application")
pwb.Visible := true
pwb.Navigate("https://autohotkey.com/boards/")
while pwb.busy or pwb.ReadyState !=4
	sleep, 10
pwb.document.getElementById("keywords").value := "Example" ; By ID
pwb.document.getElementsByClassName("button icon-button search-icon").item(0).click() ; By class name (Click)
while pwb.busy or pwb.ReadyState !=4
	sleep, 10
MsgBox, The process was successful
rockinrick28
Posts: 10
Joined: 01 Dec 2017, 23:34

Re: Click Elements by ID

02 Dec 2017, 10:38

Thats very helpful, but how can I make it click on a button on a page? What would it look like if I needed it to find a button with an ID of 61, then have it activate that button?
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Click Elements by ID

02 Dec 2017, 10:41

HIAC showed you two alternatives for identifying items. You just need to apply the click() to the one you're interested in:
pwb.document.getElementById("61").click()
HIAC
Posts: 111
Joined: 11 Oct 2017, 17:59
Location: Republic of Serbia

Re: Click Elements by ID

02 Dec 2017, 10:41

Are you sure that ID is "61"? I doubt it, it may be Index number.. however, try this.

Code: Select all

pwb.document.getElementById("61").click()
rockinrick28
Posts: 10
Joined: 01 Dec 2017, 23:34

Re: Click Elements by ID

02 Dec 2017, 11:04

I'm fairly sure that 61 is its ID, according to when i inspect the element. It still wont click the button. Does Microsoft Edge vs. Internet Explorer matter at all? Is there a way for me to just highlight the button (like what happens when you press TAB) and just have AHK use the {Enter} input instead?
HIAC
Posts: 111
Joined: 11 Oct 2017, 17:59
Location: Republic of Serbia

Re: Click Elements by ID

02 Dec 2017, 11:11

Can you show us that webpage?
rockinrick28
Posts: 10
Joined: 01 Dec 2017, 23:34

Re: Click Elements by ID

02 Dec 2017, 11:16

In this example, the ID is 54, so how would i activate it with AHK? Should I be using Internet Explorer instead of Microsoft Edge?
Attachments
Screenshot (2).png
Screenshot (2).png (160.37 KiB) Viewed 10946 times
HIAC
Posts: 111
Joined: 11 Oct 2017, 17:59
Location: Republic of Serbia

Re: Click Elements by ID

02 Dec 2017, 11:24

Yes, you should be using IE.
And, instead of Goto, use Loop.

Code: Select all

#SingleInstance, Force
OnExit, ExitSub

pwb := ComObjCreate("InternetExplorer.Application")
pwb.Visible := true
pwb.Navigate("MyWebPage")
while pwb.busy or pwb.ReadyState !=4
	sleep, 10

Loop,
{
	pwb.document.getElementById("54").click()
	while pwb.busy or pwb.ReadyState !=4
		sleep, 10
}
return

ExitSub:
pwb.quit()
return
rockinrick28
Posts: 10
Joined: 01 Dec 2017, 23:34

Re: Click Elements by ID

02 Dec 2017, 11:34

How would i modify that to make it click the button 5 times before starting again? Is there a way to make it navigate to a webpage but open up a private browser? How do I make it wait until the webpage is loaded? Thank you very much for your help so far, I appreciate it, please try to answer these three questions in one post if you can :D
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Click Elements by ID

02 Dec 2017, 12:00

Your screenshot shows that you didn't include any of the rest of what was showed to you to set up the Internet Explorer object. You just put the click line in there. It doesn't work that way. You were given a whole script for a reason. The script HIAC showed also includes the part that waits for the website to load. This is why you really can't do this if you're looking for a single command like you said in your first post. You have to learn and implement the whole approach.
rockinrick28
Posts: 10
Joined: 01 Dec 2017, 23:34

Re: Click Elements by ID

02 Dec 2017, 12:11

Ok, good to know. I figured out a different way around finding the element by its ID. However,

while pwb.busy or pwb.ReadyState !=4
sleep, 10

Are these the lines that cause it to wait?
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Click Elements by ID

02 Dec 2017, 12:16

Yes, that's correct, but it won't work if you don't set up the object.
rockinrick28
Posts: 10
Joined: 01 Dec 2017, 23:34

Re: Click Elements by ID

02 Dec 2017, 12:41

Ok, gotcha. You mean opening up the webpage with:

pwb := ComObjCreate("InternetExplorer.Application")
pwb.Visible := true
pwb.Navigate("MyWebPage")

when you say "setting up the object", right? What if i have a different way of getting to the webpage?
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Click Elements by ID  Topic is solved

02 Dec 2017, 12:47

Yes, like that. You need to use that method.
rockinrick28
Posts: 10
Joined: 01 Dec 2017, 23:34

Re: Click Elements by ID

02 Dec 2017, 12:49

Ok. thank you, I got it to work. Thank you both for your help.
rynsp8
Posts: 46
Joined: 30 Jan 2018, 12:45

Re: Click Elements by ID

07 Feb 2018, 18:26

I've got a question, and I might be able to read this in the documentation, but I'm assuming that pwb is just a variable name? Or is it a standard naming convention needed for creating and object?

And

pwb := ComObjectCreate("InternetExplorer.Application") takes Internet Explorer and places it into a manageable object to be manipulated more simply in AHK?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], RandomBoy, Rohwedder, ruespe and 380 guests