Click on a link with the mouse?

Just starting out? Need help? Post your questions and find answers here.
ricardo
Addict
Addict
Posts: 2402
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Click on a link with the mouse?

Post by ricardo »

Hi,

I need to click on a link ( lets say its the first link on a web page).

How is the best mode to achieve it?

I know how to do it in visible webgadget.
I dont need javascript clicks.
I want to do a real mouse click.

Its possible to do it no matter if the window is hidden or minimized?
I want to send some click to a hidden window.

Thanks in advance
Last edited by ricardo on Thu Apr 09, 2015 7:25 pm, edited 1 time in total.
ARGENTINA WORLD CHAMPION
User avatar
ostapas
Enthusiast
Enthusiast
Posts: 192
Joined: Thu Feb 18, 2010 11:10 pm

Re: Click on a link with the mouse?

Post by ostapas »

One way is to simulate javascript, the example below navigates to PB forum and clicks on "Search" link:

Code: Select all

OpenWindow(#PB_Any, 0, 0, 1000, 600, "", #PB_Window_SystemMenu)
WebGadget = WebGadget(#PB_Any, 0, 35, 990, 590, "http://www.purebasic.fr/english/")    
Button = ButtonGadget(#PB_Any, 7, 7, 200, 28, "Simulate click on Search")

Repeat
  Event = WindowEvent()
  If Event = #PB_Event_Gadget And EventGadget() = Button
    Javascript.s = "javascript:void(document.getElementsByTagName('a')[5].click());"
    SetGadgetText(WebGadget, Javascript)    
  EndIf
  
Until Event = #PB_Event_CloseWindow
ricardo
Addict
Addict
Posts: 2402
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: Click on a link with the mouse?

Post by ricardo »

Thanks for your answer.

Im looking a real mouse click.

I want to find if its possible (maybe with sendkeys?) to click in some x,y coordinates of the webgadget that is hidden.

Tnanks.
ARGENTINA WORLD CHAMPION
User avatar
ostapas
Enthusiast
Enthusiast
Posts: 192
Joined: Thu Feb 18, 2010 11:10 pm

Re: Click on a link with the mouse?

Post by ostapas »

Clicking links "physically" on a hidden webgadget is much more difficult than it seems, at least for me. Is it really necessary to hide the webgadget, could you just move it outside of visible desktop width, use some method to get link screen coordinates then simulate mouse click? Another way I thought about - using MS IUIAutomation interface, this way you can automate any browser or application. I could't find any PB examples, but here is AutoIt lib: http://www.autoitscript.com/forum/topic ... ome-ff-ie/
ricardo
Addict
Addict
Posts: 2402
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: Click on a link with the mouse?

Post by ricardo »

I wasa trying to do

Code: Select all

SendMessage_(WindowID(0), #WM_LBUTTONDOWN, #MK_LBUTTON, MAKELPARAM(20, 50))
SendMessage_(WindowID(0), #WM_LBUTTONUP, #MK_LBUTTON, MAKELPARAM(20, 50));
Does not work.

or

keybd_event_

Bu not sue how to handle #VK_LBUTTON
ARGENTINA WORLD CHAMPION
ricardo
Addict
Addict
Posts: 2402
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: Click on a link with the mouse?

Post by ricardo »

Hi,

For the records
i just modified and adap some Luis code (http://www.purebasic.fr/english/viewtop ... 13&t=45907) and its working:

Code: Select all

Enumeration ; windows
  #WIN_MAIN
EndEnumeration

Enumeration ; gadgets
  #WEB_MAIN
EndEnumeration


#PUSH_CTRL     = 0
#PUSH_ALT      = 1
#PUSH_SHIFT    = 2

#URL_BASE$ = "http://www.purebasic.com"
#URL_START$ = #URL_BASE$ + "/securedownload/Login.php?language="



DataSection
  
  IID_IHTMLDocument: ; {626FC520-A41E-11CF-A731-00A0C9082637}
  Data.l $626FC520
  Data.w $A41E, $11CF
  Data.b $A7, $31, $00, $A0, $C9, $08, $26, $37
  
  IID_IHTMLDocument2: ; {332C4425-26CB-11D0-B483-00C04FD90119}
  Data.l $332C4425
  Data.w $26CB, $11D0
  Data.b $B4, $83, $00, $C0, $4F, $D9, $01, $19
  
  IID_IHTMLDocument3: ; {3050F485-98B5-11CF-BB82-00AA00BDCE0B}
  Data.l $3050F485
  Data.w $98B5,$11CF
  Data.b $BB,$82,$00,$AA,$00,$BD,$CE,$0B
  
  IID_IHTMLDocument4: ; {3050F69A-98B5-11CF-BB82-00AA00BDCE0B}
  Data.l $3050F69A
  Data.w $98B5, $11CF
  Data.b $BB, $82, $00, $AA, $00, $BD, $CE, $0B
  
  IID_IHTMLDocument5: ; {3050F80C-98B5-11CF-BB82-00AA00BDCE0B}
  Data.l $3050F80C
  Data.w $98B5, $11CF
  Data.b $BB, $82, $00, $AA, $00, $BD, $CE, $0B
  
  IID_IHTMLElement: ; {3050F1FF-98B5-11CF-BB82-00AA00BDCE0B}
  Data.l $3050F1FF
  Data.w $98B5, $11CF
  Data.b $BB, $82, $00, $AA, $00, $BD, $CE, $0B
  
EndDataSection


Macro KEY_DOWN (iKey)
  keybd_event_ (iKey, 0, 0, 0)
EndMacro   

Macro KEY_UP (iKey)
  keybd_event_(iKey, 0, #KEYEVENTF_KEYUP, 0)
EndMacro   

Procedure.i WebGadget_GetHTMLDocument2 (nGadget)
  Protected oBrowser.IWebBrowser2 = GetWindowLongPtr_(GadgetID(nGadget), #GWL_USERDATA)
  Protected oDocumentDispatch.IDispatch
  Protected oHTMLDocument.IHTMLDocument2
  Protected iBusy
  
  Repeat
    While WindowEvent(): Delay(0): Wend   
    oBrowser\get_Busy(@iBusy): Delay(10)       
  Until iBusy = #VARIANT_FALSE
  
  If oBrowser
    If oBrowser\get_document(@oDocumentDispatch) = #S_OK
      If oDocumentDispatch\QueryInterface(?IID_IHTMLDocument2, @oHTMLDocument) = #S_OK
        oDocumentDispatch\Release()
      EndIf
    EndIf
  EndIf
  
  ProcedureReturn oHTMLDocument
EndProcedure

Procedure.i WebGadget_GetHTMLDocumentParent (nGadget)
  Protected oHTMLDocument.IHTMLDocument2 = WebGadget_GetHTMLDocument2 (nGadget)
  Protected oWindow.IHTMLWindow2
  
  If oHTMLDocument
    oHTMLDocument\get_parentWindow(@oWindow)
  EndIf
  
  oHTMLDocument\Release() 
  
  ProcedureReturn oWindow
EndProcedure

Procedure WebGadget_SetFocus (nGadget)
  Protected oWindow.IHTMLWindow2 = WebGadget_GetHTMLDocumentParent (nGadget)
  
  If oWindow   
    oWindow\focus()
    oWindow\Release()
  EndIf
EndProcedure

Procedure WebGadget_ExecScript (nGadget, sScriptCode.s, sScriptLanguage.s = "JavaScript")   
  Protected oWindow.IHTMLWindow2 = WebGadget_GetHTMLDocumentParent (nGadget)
  Protected tVariant.VARIANT
  
  If oWindow
    oWindow\execScript (sScriptCode, sScriptLanguage, @tVariant)
    oWindow\Release()
  EndIf   
EndProcedure

Procedure WebGadget_SetFocusByName (nGadget, sFieldName.s)
  Protected sScript.s
  
  sScript = "var elements = document.getElementsByName('"+ sFieldName + "');"
  sScript + "for (var i = 0; i < elements.length; ++i)"
  sScript + "{"
  sScript + "if (elements[i].tagName.toLowerCase() == 'input')"
  sScript + "{"
  sScript + "elements[i].focus();"
  sScript + "break;"
  sScript + "}"
  sScript + "}"
  
  WebGadget_ExecScript (nGadget, sScript)
EndProcedure

Procedure WebGadget_SelectLink (nGadget, index)
  Protected sScript.s
  
  sScript = "var elements = document.all.tags('a')(" + Str(index) + ");"
  sScript + "elements.focus();"
  
  
  WebGadget_ExecScript (nGadget, sScript)
EndProcedure


Procedure PushKey (iKey, iMod = 0)
  
  If iMod & #PUSH_CTRL
    KEY_DOWN (#VK_CONTROL)
  EndIf
  
  If iMod & #PUSH_SHIFT
    KEY_DOWN (#VK_SHIFT)
  EndIf
  
  If iMod & #PUSH_ALT
    KEY_DOWN (#VK_MENU)
  EndIf
  
  If iKey
    KEY_DOWN (iKey)
    KEY_UP (iKey)
  EndIf
  
  If iMod & #PUSH_ALT
    KEY_UP (#VK_MENU)
  EndIf
  
  If iMod & #PUSH_SHIFT
    KEY_UP (#VK_SHIFT)
  EndIf
  
  If iMod & #PUSH_CTRL
    KEY_UP (#VK_CONTROL)
  EndIf
EndProcedure

Procedure Main()
  
  Protected iRetVal, iEvent, iStepCount
  Protected sURL.s
  
  iRetVal = OpenWindow(#WIN_MAIN, 10, 10, 800, 600, "WebRobot", #PB_Window_SystemMenu)
  
  iRetVal = WebGadget(#WEB_MAIN, 5, 5, 790, 550, #URL_START$)     
  myBrowser.IWebBrowser2 = GetWindowLong_(GadgetID(0), #GWL_USERDATA)
  myBrowser\put_Silent(#True)
  Repeat
    iEvent = WaitWindowEvent(10)
    
    
    
    Select iEvent
      Case #PB_Event_Gadget
        Select EventGadget()                                   
          Case #WEB_MAIN
            Select EventType()
              Case #PB_EventType_DownloadEnd                           
                iStepCount + 1
                Select iStepCount
                  Case 1:
                    
                    ;To click a link just select the number of the link, added by Ricardo
                    WebGadget_SetFocus(#WEB_MAIN)
                    WebGadget_SelectLink(#WEB_MAIN, 3)
                    PushKey(#VK_RETURN)
                    
                  Case 2:
                    Debug "Ended."
                    HideWindow(#WIN_MAIN,#False)
                EndSelect
            EndSelect                   
        EndSelect
    EndSelect
    
  Until iEvent = #PB_Event_CloseWindow
  
EndProcedure


Main()

ARGENTINA WORLD CHAMPION
Xland
New User
New User
Posts: 7
Joined: Sun Jul 15, 2018 7:42 am

Re: Click on a link with the mouse?

Post by Xland »

ostapas wrote:One way is to simulate javascript, the example below navigates to PB forum and clicks on "Search" link:

Code: Select all

OpenWindow(#PB_Any, 0, 0, 1000, 600, "", #PB_Window_SystemMenu)
WebGadget = WebGadget(#PB_Any, 0, 35, 990, 590, "http://www.purebasic.fr/english/")    
Button = ButtonGadget(#PB_Any, 7, 7, 200, 28, "Simulate click on Search")

Repeat
  Event = WindowEvent()
  If Event = #PB_Event_Gadget And EventGadget() = Button
    Javascript.s = "javascript:void(document.getElementsByTagName('a')[5].click());"
    SetGadgetText(WebGadget, Javascript)    
  EndIf
  
Until Event = #PB_Event_CloseWindow
here is an example of a button click

And how will it be right to click, with some random delay in time
on the link in the window for the web gadget?
Post Reply