Click on specific button of existing web page.

Get help with using AutoHotkey and its commands and hotkeys
teadrinker
Posts: 2065
Joined: 29 Mar 2015, 09:41
Contact:

Re: Click on specific button of existing web page.

26 Jul 2019, 19:13

tank wrote: No it returns the parentwindow property of document object
But your class iWebBrowser2 does not contain such property. Can you explain in more detail?
malcev
Posts: 834
Joined: 12 Aug 2014, 12:37

Re: Click on specific button of existing web page.

26 Jul 2019, 19:45

tank wrote:
26 Jul 2019, 18:43
frames
What do You mean by "frames"?
Can You give example when IEGet() will not work with existing DOM of IE?

Code: Select all

Pwb:= ComObjCreate( "InternetExplorer.Application" )
Pwb.Navigate("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_frame_cols")
Pwb.Visible:= True
While ( Pwb.Busy || Pwb.ReadyState != 4 )
   Sleep 10	
msgbox page loaded

oIE := IEGet()
msgbox % oIE.document.documentElement.OuterHtml

iFrame := ComObject( 9, ComObjQuery(oIE.document.parentWindow.frames["iframeResult"],"{332C4427-26CB-11D0-B483-00C04FD90119}","{332C4427-26CB-11D0-B483-00C04FD90119}"), 1)
msgbox % iFrame[0].document.documentElement.OuterHtml



IEGet(name="") {
   IfEqual, Name,, WinGetTitle, Name, ahk_class IEFrame     ;// Get active window if no parameter
   Name := (Name="New Tab - Windows Internet Explorer")? "about:Tabs":RegExReplace(Name, " - (Windows|Microsoft)? ?Internet Explorer$")
   for wb in ComObjCreate("Shell.Application").Windows()
      if wb.LocationName=Name and InStr(wb.FullName, "iexplore.exe")
         return wb
} 
User avatar
tank
Posts: 2855
Joined: 28 Sep 2013, 22:15
Facebook: charlie.simmons.7334
Google: ttnnkkrr
GitHub: ttnnkkrr
Location: Irving TX
Contact:

Re: Click on specific button of existing web page.

26 Jul 2019, 19:50

teadrinker wrote:
26 Jul 2019, 19:13
tank wrote: No it returns the parentwindow property of document object
But your class iWebBrowser2 does not contain such property. Can you explain in more detail?
pDoc(oIE) queries the parent window from the document object
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
https://www.facebook.com/ahkscript.org
If you have forum suggestions please submit a pull request
Check Out WebWriter
Thanks Tank :thumbup:
teadrinker
Posts: 2065
Joined: 29 Mar 2015, 09:41
Contact:

Re: Click on specific button of existing web page.

26 Jul 2019, 20:56

tank wrote: pDoc(oIE) queries the parent window from the document object
But your code does not return the parent window from the document object, it returns oIE which is oIE := window, which is obtained by enumerating ComObjCreate("Shell.Application").Windows. Am I wrong?

@malcev
The same without object query:

Code: Select all

Pwb:= ComObjCreate( "InternetExplorer.Application" )
Pwb.Navigate("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_frame_cols")
Pwb.Visible:= True
While ( Pwb.Busy || Pwb.ReadyState != 4 )
   Sleep 10   
msgbox page loaded

oIE := IEGet()
parentFrame := oIE.document.getElementById("iframeResult")
MsgBox, % parentFrame.contentDocument.querySelector("[src='frame_a.htm']").contentDocument.documentElement.OuterHtml

IEGet(name="") {
   IfEqual, Name,, WinGetTitle, Name, ahk_class IEFrame     ;// Get active window if no parameter
   Name := (Name="New Tab - Windows Internet Explorer")? "about:Tabs":RegExReplace(Name, " - (Windows|Microsoft)? ?Internet Explorer$")
   for wb in ComObjCreate("Shell.Application").Windows()
      if wb.LocationName=Name and InStr(wb.FullName, "iexplore.exe")
         return wb
}
malcev
Posts: 834
Joined: 12 Aug 2014, 12:37

Re: Click on specific button of existing web page.

27 Jul 2019, 08:25

teadrinker, Your method works not always.
Try to get this iframe with contentDocument:

Code: Select all

Pwb:= ComObjCreate( "InternetExplorer.Application" )
Pwb.Navigate("https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#Example2")
Pwb.Visible:= True
While ( Pwb.Busy || Pwb.ReadyState != 4 )
   Sleep 10	

oIE := IEGet()
iFrame := ComObject( 9, ComObjQuery(oIE.document.parentWindow.frames["frame_Example2"],"{332C4427-26CB-11D0-B483-00C04FD90119}","{332C4427-26CB-11D0-B483-00C04FD90119}"), 1)
msgbox % iFrame[0].document.documentElement.OuterHtml


IEGet(name="") {
   IfEqual, Name,, WinGetTitle, Name, ahk_class IEFrame     ;// Get active window if no parameter
   Name := (Name="New Tab - Windows Internet Explorer")? "about:Tabs":RegExReplace(Name, " - (Windows|Microsoft)? ?Internet Explorer$")
   for wb in ComObjCreate("Shell.Application").Windows()
      if wb.LocationName=Name and InStr(wb.FullName, "iexplore.exe")
         return wb
} 
User avatar
tank
Posts: 2855
Joined: 28 Sep 2013, 22:15
Facebook: charlie.simmons.7334
Google: ttnnkkrr
GitHub: ttnnkkrr
Location: Irving TX
Contact:

Re: Click on specific button of existing web page.

29 Jul 2019, 09:16

teadrinker wrote:
26 Jul 2019, 20:56
Am I wrong?
yes you are wrong
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
https://www.facebook.com/ahkscript.org
If you have forum suggestions please submit a pull request
Check Out WebWriter
Thanks Tank :thumbup:
teadrinker
Posts: 2065
Joined: 29 Mar 2015, 09:41
Contact:

Re: Click on specific button of existing web page.

29 Jul 2019, 09:42

teadrinker wrote: Is there a way to see the difference between my window and yours?
?
User avatar
rommmcek
Posts: 1190
Joined: 15 Aug 2014, 15:18

Re: Click on specific button of existing web page.

29 Jul 2019, 12:22

Interesting discussion of the extended topic.

@Toni Osuna:
Using tank's function:

Code: Select all

#NoEnv
#SingleInstance, Force

;url := "https www.google.com /"  ;Broken Link for safety
url := "https://www.google.com/"

IfWinNotExist, ahk_class IEFrame
{
    Run, iexplore.exe %url%,,, PID
    WinWait, ahk_pid %PID%,, 5
} else {
    WinActivate, ahk_class IEFrame
    WinWaitActive, ahk_class IEFrame
    ;WinGetActiveTitle, ieWinTitle ; didn't work using "title"
    ControlGetText, url, Edit1, ahk_class IEFrame ; to get url programmatically see https://www.autohotkey.com/boards/viewtopic.php?f=6&t=3702
}

start := A_TickCount
while !(wb := GetIePagePwb("url", url)) && A_TickCount - start < 10000
   Sleep, 25

if !wb {
   MsgBox, Failed to get wb object by url
   Return
}

wb.Navigate("https://www.autohotkey.com/", navOpenInNewTab := 2048)
wb := ""

start := A_TickCount
while !(wb := GetIePagePwb("title", "AutoHotkey")) && A_TickCount - start < 10000
   Sleep, 25

if !wb {
   MsgBox, Failed to get wb object by title
   Return
}

While (wb.document.getElementsByClassName("mbr-buttons__btn").(1).InnerText != "Forums")
    Sleep, 25

wb.document.getElementsByClassName("mbr-buttons__btn").(1).Click()	;Autohotkey forums button (searched by Class)
Return

GetIePagePwb(param, value) {
   if !(param ~= "i)^(url|title)$")
      throw Exception("Pameter " . param . " not supported")
   nil:="", (param = "url" && param := "LocationURL")
   for window in ComObjCreate("Shell.Application").Windows {
      WinGetClass, winClass, % "ahk_id" window.hwnd
      if (winClass != "IEFrame")
         continue
      try doc := window.document
      catch
         break
      obj := {LocationURL: window, title: doc}[param]
      if (obj[param] = value && found := true)
         break
   }
   if found
      Return window
}
Using Jethrow's function:

Code: Select all

#NoEnv
#SingleInstance, Force

;url := "https www.google.com /"  ;Broken Link for safety
url := "https://www.google.com/"

IfWinNotExist, ahk_class IEFrame
{
	wb := ComObjCreate("InternetExplorer.Application")
	wb.Visible := true
	wb.Navigate( url )
} else {
    WinActivate, ahk_class IEFrame
    WinWaitActive, ahk_class IEFrame
    wb := pwb_get()
}

wb.Navigate("https://www.autohotkey.com/", navOpenInNewTab := 2048)

while !WinActive("AutoHotkey")
	sleep, 25

wb := pwb_get()

While (wb.document.getElementsByClassName("mbr-buttons__btn").(1).InnerText != "Forums")
    Sleep, 25

wb.document.getElementsByClassName("mbr-buttons__btn").(1).Click()	;Autohotkey forums button (searched by Class)
Return

PWB_Get(WinTitle="A", Svr#=1) ; Jethrow - http://www.autohotkey.com/board/topic/47052-basic-webpage-controls-with-javascript-com-tutorial/
{
	Static msg := DllCall("RegisterWindowMessage", "str", "WM_HTML_GETOBJECT")
	, IID := "{0002DF05-0000-0000-C000-000000000046}" ; IID_IWebBrowserApp
	;,IID := "{332C4427-26CB-11D0-B483-00C04FD90119}" ; IID_IHTMLWindow2
	SendMessage, msg, 0, 0, Internet Explorer_Server%Svr#%, %WinTitle%
	If (ErrorLevel != "FAIL") {
		lResult := ErrorLevel, VarSetCapacity(GUID, 16, 0)
		If (DllCall("ole32\CLSIDFromString", "wstr", "{332C4425-26CB-11D0-B483-00C04FD90119}", "ptr", &GUID) >= 0) {
			DllCall("oleacc\ObjectFromLresult", "ptr", lResult, "ptr", &GUID, "ptr", 0, "ptr*", pdoc)
			Return ComObj(9, ComObjQuery(pdoc, IID, IID), 1), ObjRelease(pdoc)
		}
	}
	MsgBox, 262160, %A_ScriptName% - %A_ThisFunc%(): Error,  Unable to obtain browser object (PWB) from window:`n`n%WinTitle%
}
owem
Posts: 6
Joined: 04 Sep 2019, 14:22

Re: Click on specific button of existing web page.

05 Sep 2019, 09:28

Using:

Code: Select all

url := "https www.google.com /"  Broken Link for safety
Run, iexplore.exe %url%,,, PID
WinWait, ahk_pid %PID%

start := A_TickCount
while !(wb := GetIePagePwb("url", url)) && A_TickCount - start < 10000
   Sleep, 100

if !wb {
   MsgBox, Failed to get wb object by url
   Return
}
wb.Navigate("https://www.autohotkey.com/", navOpenInNewTab := 2048)
wb := ""

start := A_TickCount
while !(wb := GetIePagePwb("title", "AutoHotkey")) && A_TickCount - start < 10000
   Sleep, 100

if !wb {
   MsgBox, Failed to get wb object by title
   Return
}
wb.document.getElementsByClassName("mbr-buttons__btn").(1).Click()

GetIePagePwb(param, value) {
   if !(param ~= "i)^(url|title)$")
      throw Exception("Pameter " . param . " not supported")
   (param = "url" && param := "LocationURL")
   for window in ComObjCreate("Shell.Application").Windows {
      WinGetClass, winClass, % "ahk_id" window.hwnd
      if (winClass != "IEFrame")
         continue
      try doc := window.document
      catch
         break
      obj := {LocationURL: window, title: doc}[param]
      if (obj[param] = value && found := true)
         break
   }
   if found
      Return window
}
Thank you for this code.
Btw, How to do this on Chrome ?
teadrinker
Posts: 2065
Joined: 29 Mar 2015, 09:41
Contact:

Re: Click on specific button of existing web page.

05 Sep 2019, 10:47

This requires a different, more complex approach.
Chrome.ahk
dg2019
Posts: 1
Joined: 25 Jul 2019, 13:59

Re: Click on specific button of existing web page.

11 Aug 2020, 13:57

Did you ever find a working script to click on a button of an existing webpage? I am very new to coding and would appreciate seeing a full example of such a script. The last script I see brings up a new page, but I want to click on a button of a existing webpage already on my screen. Thanks for your help

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Bodhi, Spawnova, XMCQCX and 34 guests