Click on specific button of existing web page.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
teadrinker
Posts: 4326
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: 1769
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: 3122
Joined: 28 Sep 2013, 22:15
Location: CarrolltonTX
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;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter
teadrinker
Posts: 4326
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: 1769
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: 3122
Joined: 28 Sep 2013, 22:15
Location: CarrolltonTX
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;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter
teadrinker
Posts: 4326
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: 1474
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: 4326
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
Janusz
Posts: 89
Joined: 18 Dec 2020, 17:47

Re: Click on specific button of existing web page.

07 Apr 2021, 01:29

Thank you for yours very useful and complex codes. I would like to emphasize very useful complex .ahk library which uses also some WIndows library calls to access WEB elements. You could even use those techniques to create screen readers in Autohotkey by browsing Internet WEB page to create virtual buffer. I have big plea to you. How to activate some link with Play name, which do not contain direct link, it is link to activate HTML5 player. By other words, I would had to find Play link and simulate clicking on it. But it has hook. Internet Explorer page window is hydden and page is loaded before I would want to activate Play link. The question is, if is it possible to click on WEB element, if IE WIndow is hydden with no need to unhyde it. I AM constructing Internet radio player, which call official WEB site. I do not want to determine real streams link, since it is not wanted by many radio stations prowiders, it is causing hydden war between users and streaming servers admins, who are monitoring access protocols and I do not want to go by this way. i want to discuss with official streams dedevelopers instead so they would give Me permissions to use their official pages without hacking direct sstreams links. The reason, whx I AM constructing The alternate GUI to control their pages? Severly physically impaired people who has also sighted problems have issues when pressing keys for many times while using official sites.
I will respect licensesconditions of streams prowiders.
But how to click on Play link, it is link which do not contain some real link to copy by using contex menu. Or should I rather try to use Javascript to click on it, if IE window is hydden?
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Click on specific button of existing web page.

07 Apr 2021, 11:25

@Janusz
It's possible, but it depends on a web page.

Code: Select all

html =
(
   <!DOCTYPE html>
   <html>
   <head>
      <meta http-equiv="X-UA-Compatible" content="IE=edge" />
   </head>
   <body>
      <script>function showAlert(e) { e.preventDefault(); alert("the link was clicked!"); }</script>
      <a href="" onclick="showAlert(event)">Click Me</a>
   </body>
   </html>
)
IE := ComObjCreate("InternetExplorer.Application")
OnExit( Func("Exit").Bind(IE) )

IE.Visible := false
IE.navigate("about:blank")
while IE.busy
   continue
IE.document.write(html)

IE.document.querySelector("a").click()

Exit(IE)  {
   try IE.Quit()
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: coder96, Google [Bot], RandomBoy, Rohwedder and 177 guests