Jump to content

SOLVED - Clicking button on javascript window


Italiano
 Share

Recommended Posts

Hi... before all, sorry for my bad english, i am italian.

My problem is probably easy to fix but even if i checked the forum and i found similar issues, i could not solve... there is a very similar question just some days ago with this topic --> https://www.autoitscript.com/forum/topic/179487-how-to-click-ok-in-messagebox/ I tried to do the same things, but  i was not good enough :(

I explain better: I want to do a script to reboot my router, this is the scenario...

-Windows 7 ultimate 32 bit

-Internet Explorer 11

-Router TP-Link w8970

 

Script looks like this

 

#include <IE.au3>

Local $oUser, $oPass, $oSubmit
Local $sUser = "admin"
Local $sPass = "admin"
Local $url = "192.168.1.1"
Local $oIE = _IECreate($url, 1)
Local $URLRestart = "http://192.168.1.1/#__restart.htm"


$o_login = _IEGetObjById($oIE,"userName")
_IEFormElementSetValue ($o_login, $sUser)
$o_password = _IEGetObjByID($oIE,"pcPassword")
_IEFormElementSetValue ($o_password, $sPass)

$o_signin=_IEGetObjById($oIE,"loginBtn")
_IEAction($o_signin,"click")

_IENavigate($oIE, $URLRestart)
$o_reboot=_IEGetObjById($oIE,"button_reboot")
_IEAction($o_reboot,"click")

_IELoadWait($oIE)
WinWaitActive("[CLASS:#32770]", "")
ControlClick ("[CLASS:#32770]", "OK", 1, "left", 1)

So, script simply do a login, it goes to the page with the reboot button, it clicks on the button and it appears a window (from a javascript, i think) with a question. Script is not able to click on the button in this window. So basically everything works okay until the "_IEAction($o_reboot,"click")",

catturaautoitl7ry5.png

 

As you can image, the script is not able to click the "OK" button. This is what i get using the au3info.

catturaautoit2ivrf1.png

 

 

I did a lot of tries using "winwait" in place of "winwaitactive" and using the title of the window ("Messaggio dalla pagina web") in place of the class #32770. I tried also to send a simple "enter" in place of the click but i feel that the problem is before with the winwaitactive... The window appears istantaneously and maybe it's so quick that winwait can not intercept it. I tried to put also a sleep after the click but, of course, did not work.

 

I suppose i am missing something easy... if anyone would be so gentle to help, i would be grateful :)

And sorry again for my weak english!

 

 

 

Edited by Italiano
Link to comment
Share on other sites

Strange, I tested the javascript's confirm on Chrome and IE and both initialized the windows with the activated state.

Did you try to winactive with part of the title message? (Ex: WinActive("Messaggio"))

Did you try to remove _IELoadWait and wait until the page pops up?

I am newbie btw.

Link to comment
Share on other sites

thanks for your answer... but i already tried wht you adviced...

I add some other information. As i said, the script is not able to click the "OK" button and it is, let's say, in standby... if i manually close the window with "X" or pressing the button "annulla" and then I manually click the "Reboot" button, guess what? the script goes on and automatically click the "ok" button and it reboots the router. Since i am noob, i am not sure about what is wrong... to me it looks like the window misses the focus, i tried to put a "winactivate" after the winwaitactive but nothing changed. Since the script works if i manually close the window and i click the "reboot" button, I am pretty sure that the parameters are correct in the winwaitactive...

One other thing: the window title "Messaggio dalla pagina web" appears just in internet explorer... in firefox there is no title and i get the simple message inside "The device will reboot" etc etc.

please, anyone could help...?  I know that if you do not have this router, you can not replicate the issue but I think it is something easy to fix for the experts of Autoit...

Thx in advance again!

Link to comment
Share on other sites

  • Developers

Try:

WinWait("Messagio dalla pagina Web", "")
ControlClick ("Messagio dalla pagina Web", "", "Button1", "left", 1)

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

Does it pass the WinWait and execute the ControlClick?
Just add a MsgBox() right after the WinWait() to check when it valid to assure it performs the ControlClick() at the right moment.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Does it pass the WinWait and execute the ControlClick?
Just add a MsgBox() right after the WinWait() to check when it valid to assure it performs the ControlClick() at the right moment.

Jos

I guess i do not speak english so good to explain better... if i manually close the window with "X" or pressing the button "annulla" and then I manually click the "Reboot" button the script goes on and automatically click the "ok" button and it reboots the router, so the controlclick works for sure...  the exact code that i wrote (and also the code that you wrote) works ONLY if i manually close the window with buttons "OK" and "Annulla" and i manually make it appear again, pressing the reboot button on the main page... looks like winwait can not detect that window automatically... but it does (with same code) after manual clicks...

Sorry if i can not explain better :(

Edited by Italiano
Link to comment
Share on other sites

  • Developers

Just start doing some debugging by putting in MsgBox() statements in your  script to check if it is performing the rights statements at the right time.

Jos  

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

@mikell, I removed that, nothing changed. Thanks for effort tough :)

@Jos: I tried with 2 msgbox. First msbox appears when it should. Second one, not... it appears istantaneously the window with buttons "OK" and "Annulla". When i manually close this window, then the msgbox appears...

$o_reboot=_IEGetObjById($oIE,"button_reboot")

msgbox(0, "PROBLEMA", "PROBLEMA1")
_IEAction($o_reboot,"click")
 msgbox(0, "PROBLEMA", "PROBLEMA2")
sleep(5000)

WinWait("Messaggio dalla pagina Web", "")


ControlClick ("Messaggio dalla pagina Web", "", "Button1", "left", 1)

 

 

Link to comment
Share on other sites

  • Developers

So you are saying that the MsgBox with Problema2 only appears after you close the "The device will reboot" window?

Maybe try this version and see/tell us what happens:

$o_reboot=_IEGetObjById($oIE,"button_reboot")

msgbox(0, "PROBLEMA", "PROBLEMA1")
_IEAction($o_reboot,"click")
msgbox(0, "PROBLEMA", "PROBLEMA2")
WinWait("Messaggio dalla pagina Web", "")
msgbox(0, "PROBLEMA", "PROBLEMA3")
ControlClick ("Messaggio dalla pagina Web", "", "Button1", "left", 1)
msgbox(0, "PROBLEMA", "PROBLEMA4")

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

A brute way you could try is to adlibregister a func at the top of the script

Adlibregister("test")
;....
Func test()
   Local $w = WinList("[CLASS:#32770]")
   For $i = 1 to $w[0][0]
      If StringInStr(WinGetTitle($w[$i][1]), "Messaggio") Then 
            WinActivate($w[$i][1])
            ControlClick ($w[$i][1], "", "Button1", "left", 1)
      EndIf
  Next
EndFunc

 

Link to comment
Share on other sites

Link to comment
Share on other sites

I already ran into this problem before.
The problem is that _IEAction ($o_reboot, "click") cause the blocking of the script until the popup is closed manually.
Here a possible solution: https://www.autoitscript.com/forum/topic/154405-help-regarding-webpage-dialog-in-ie/?do=findComment&comment=1114987

in short, instead of using _IEAction ($o_reboot, "click") you have to use something like this:

_IEAction ($O_reboot, "focus"); set focus to button
ControlSend(_IEPropertyGet($Oie, "hwnd"), "", "", "{ENTER}"); send "enter"


This avoids blocking of the script and the subsequent instructions are executed

Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

I meant something like this.

#include <APISysConstants.au3>
#include <GUIMenu.au3>
#include <WinAPIProc.au3>
#include <WinAPISys.au3>
#include <WindowsConstants.au3>
#include <IE.au3>


Local $hEventProc = DllCallbackRegister('_EventProc', 'none', 'ptr;dword;hwnd;long;long;dword;dword')
Global $g_tRECT, $g_iIndex, $g_hMenu = 0
OnAutoItExitRegister('OnAutoItExit')
Local $hEventHook = _WinAPI_SetWinEventHook($EVENT_SYSTEM_ALERT, $EVENT_SYSTEM_ALERT, DllCallbackGetPtr($hEventProc))


Local $oUser, $oPass, $oSubmit
Local $sUser = "admin"
Local $sPass = "admin"
Local $url = "192.168.1.1"
Local $oIE = _IECreate($url, 1)
Local $URLRestart = "http://192.168.1.1/#__restart.htm"


$o_login = _IEGetObjById($oIE, "userName")
_IEFormElementSetValue($o_login, $sUser)
$o_password = _IEGetObjById($oIE, "pcPassword")
_IEFormElementSetValue($o_password, $sPass)

$o_signin = _IEGetObjById($oIE, "loginBtn")
_IEAction($o_signin, "click")

_IENavigate($oIE, $URLRestart)
$o_reboot = _IEGetObjById($oIE, "button_reboot")
_IEAction($o_reboot, "click")

_IELoadWait($oIE)



While 1
    Sleep(1000)
WEnd


Func OnAutoItExit()
    _WinAPI_UnhookWinEvent($hEventHook)
    DllCallbackFree($hEventProc)
EndFunc   ;==>OnAutoItExit


Func _EventProc($hEventHook, $iEvent, $hWnd, $iObjectID, $iChildID, $iThreadId, $iEventTime)
    #forceref $hEventHook, $iObjectID, $iChildID, $iThreadId, $iEventTime

    If _WinAPI_GetProcessName(WinGetProcess($hWnd)) = "iexplore.exe" And WinGetTitle($hWnd) = "Messaggio dalla pagina web" Then
        ControlClick($hWnd, "", "Button1")
    EndIf


EndFunc   ;==>_EventProc

Saludos

Link to comment
Share on other sites

Chimp won ))))  using his code everything works fantastic! Thank you a lot, i would never think about that!

 

@Mikell and Danyfirex: thx to both for your time and your effort :)

 

This can be closed or tagged as SOLVED ;-)

 

--------------------------------------------------------------------------

By the way, the final script is this:

 

#include <IE.au3>

Local $oUser, $oPass, $oSubmit
Local $sUser = "admin"
Local $sPass = "Mypassword"
Local $url = "192.168.1.1"
Local $oIE = _IECreate($url, 0,1)
Local $URLRestart = "http://192.168.1.1/#__restart.htm"

Logout()

Func Riavvio()
$o_login = _IEGetObjById($oIE,"userName")

_IEFormElementSetValue ($o_login, $sUser)
$o_password = _IEGetObjByID($oIE,"pcPassword")
_IEFormElementSetValue ($o_password, $sPass)

$o_signin=_IEGetObjById($oIE,"loginBtn")
_IEAction($o_signin,"click")

_IENavigate($oIE, $URLRestart)
$o_reboot=_IEGetObjById($oIE,"button_reboot")



_IEAction ($o_reboot, "focus")
ControlSend(_IEPropertyGet($Oie, "hwnd"), "", "", "{ENTER}");

WinWaitActive("[CLASS:#32770]", "")
ControlClick ("[CLASS:#32770]", "OK", 1, "left", 1)
_IELoadWait($oIE)
sleep(100)
_IEQuit($oIE)

EndFunc



Func Logout()

   Local $oLogout = _IEGetObjByID($oIE, "menu_logout")
   if @error Then
      Riavvio()
   else

   Local $hWnd = _IEPropertyGet($oIE, "hwnd")
   _IEAction($oLogout, "focus")
      ControlSend($hWnd, "", "", "{Enter}")

   WinWaitActive("[CLASS:#32770]", "")
   ControlClick ("[CLASS:#32770]", "OK", 1, "left", 1)
EndIf

sleep(100)
Riavvio()

EndFunc

it works for router TP-LINK w8970 but probably works also on other TP-LINK routers :)

Edited by Italiano
Insert final script
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...