AutoHotkey Community

It is currently May 25th, 2012, 10:30 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 25 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: November 27th, 2007, 6:31 pm 
Offline

Joined: October 22nd, 2007, 3:44 pm
Posts: 11
Hi,
I am working on some automation tasks on webpages. I have gotten very far with Sean's excellent COM.ahk (thanks for that !) and have now gotten to a point, where it would be cool if I could use javascript to get a value from a page and save it to a variable. Is that possible ?

Here an example:

On http://www.mredkj.com/tutorials/tutorial005.html, I can use the following to get an alert box with the value of the first selection item:

Code:
#Include COM.ahk

COM_CoInitialize()
pweb := COM_Createobject("InternetExplorer.Application")
COM_Invoke(pweb, "Visible=", "True")
COM_Invoke(pweb, "Navigate", "http://www.mredkj.com/tutorials/tutorial005.html")
sleep 3000
COM_Invoke(pweb, "Navigate", "javascript:alert(parent.document.forms[1].elements[2].options[0].value)")

return


Now my question, is it possible to store that value somehow ?
What I need for ma automation tast, is an if - statement, that looks something like this (pseudo code):

if (selectbox has only one value)
dostuff
else
dosomething different.

Is something like that possible ?

Thanks!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 28th, 2007, 12:40 am 
Offline

Joined: February 9th, 2006, 8:36 pm
Posts: 338
just a guess - javascript can 'documentwrite' anything probably, so make your js put the variable value into some invible tag, then read and parse current page contents via some COM :-)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 28th, 2007, 1:29 am 
Code:
javascript:var o=document.documentElement.innerHTML;window.clipboardData.setData("Text",o);void(0);
Code:
javascript:var o=document.documentElement.innerText;window.clipboardData.setData("Text",o);void(0);
or
Code:
javascript:void(window.clipboardData.setData('Text', document.element.innerText));


Check/parse the clipboards content afterwards. Hope that helps :)


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 28th, 2007, 1:37 am 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
May append this at the end of the script. Wow, looks scary. ;)
Code:
Value := COM_Invoke(COM_Invoke(COM_Invoke(COM_Invoke(COM_Invoke(COM_Invoke(COM_Invoke(COM_Invoke(pweb,"Document"),"forms"),"Item",1),"elements"),"Item",2),"options"),"Item",0),"value")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 28th, 2007, 2:27 am 
Offline

Joined: February 9th, 2006, 8:36 pm
Posts: 338
how about this (not tested):
Code:
Value := COM_InvokeDeep( pweb, "Document.forms.Item[1].elements.Item[2].options.Item[0].value" )

COM_InvokeDeep(obj,path)
{
   res:=obj
   loop,parse,path,.,]
   {  prop=%A_LoopField%
      value=
      StringGetPos,i,A_LoopField,[
      IfEqual,ErrorLevel,0
      {   StringLeft,prop,A_LoopField,%i%
         StringMid,value,A_LoopField,% i+2
      }
      res:=COM_Invoke( res, prop, value)
   }
   return res
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 28th, 2007, 12:12 pm 
Offline

Joined: October 22nd, 2007, 3:44 pm
Posts: 11
Wow, cool stuff. Thanks for the pointers !


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 3rd, 2007, 3:53 pm 
Offline

Joined: March 16th, 2005, 10:33 pm
Posts: 968
Location: Frisia
Tested (adapted COM_InvokeDeep):

Code:
#Include %A_ScriptDir%\COM.ahk

COM_CoInitialize()
pweb := COM_Createobject("InternetExplorer.Application")
COM_Invoke(pweb, "Visible=", "True")
COM_Invoke(pweb, "Navigate", "http://www.mredkj.com/tutorials/tutorial005.html")
Sleep 3000

MsgBox % COM_InvokeDeep( pweb, "document.forms[1].elements[2].options[0].value" )


COM_InvokeDeep( obj, path )
{
   res := obj
   Loop, Parse, Path, ., ]
   {
         prop := A_LoopField
      value =
      StringGetPos, i, A_LoopField, [
      IfEqual, ErrorLevel, 0 ; contains index
      {
            StringLeft, prop, A_LoopField, %i%
         StringMid, value, A_LoopField, % i+2
      }
         If (value != "") ; contains index
         {
            res := COM_Invoke( res, prop )
            res := COM_Invoke( res, "Item", value )
         }
         Else
         {
           res := COM_Invoke( res, prop )
         }
   }
   Return res
}


Return

_________________
Image mirror 1mirror 2mirror 3ahk4.me • PM or Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 3rd, 2007, 4:46 pm 
Offline

Joined: March 16th, 2005, 10:33 pm
Posts: 968
Location: Frisia
Hmm...

This one works:

Code:
MsgBox % COM_InvokeDeep( pweb, "document.documentElement.innerHTML" )


This one doesn't:

Code:
MsgBox % COM_InvokeDeep( pweb, "document.getElementById('topbanner').innerHTML" )


What is needed to make the second one work? COM experts?

_________________
Image mirror 1mirror 2mirror 3ahk4.me • PM or Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 4th, 2007, 4:28 pm 
Offline

Joined: March 16th, 2005, 10:33 pm
Posts: 968
Location: Frisia
Bump...

Anybody?

_________________
Image mirror 1mirror 2mirror 3ahk4.me • PM or Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 6th, 2007, 2:24 am 
Offline

Joined: June 26th, 2006, 6:14 pm
Posts: 1379
Location: USA
Quote:
getElementById('topbanner')
is a javascript function. i doubt it is supported by COM the way you are calling it.

_________________
Image
ʞɔпɟ əɥʇ ʇɐɥʍ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 6th, 2007, 2:33 am 
Offline

Joined: June 26th, 2006, 6:14 pm
Posts: 1379
Location: USA
2 hotzenpl0tz
The following will get the info you want into an ahk variable.

Code:
MsgBox % JSVAR := IE_InjectJS(WinExist("A"), "javascript:var ahkvar1 = parent.document.forms[1].elements[2].options[0].value;", "ahkvar1")


You can find the function here:
http://www.autohotkey.com/forum/viewtopic.php?t=25473


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 6th, 2007, 6:26 pm 
Offline

Joined: October 22nd, 2007, 3:44 pm
Posts: 11
ahklerner: Yes, thanks alot. With the help of the examples above, I was able to adapt it to my given problem and was able to automate a process that had different routes depending of the state of a select object on the page. As soon as I am finished with everything, I am planning to find some random webpage on the net with the elements I have used and writing a simple script, that automates some things on that webpage in order to give beginners some (commentated) insight on how certain things can be done to achieve these things.
The problem is, at the moment I am learning a new trick every day, and am rewriting my script at work almost every time I have some time to do so - so this might take some time :) But helping ahk beginners who are interested in IE automation tasks would be a good way to give back to the community here, a really great community btw. !


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 6th, 2007, 6:36 pm 
Offline

Joined: March 16th, 2005, 10:33 pm
Posts: 968
Location: Frisia
Quote:
is a javascript function. i doubt it is supported by COM the way you are calling it.


Darn

_________________
Image mirror 1mirror 2mirror 3ahk4.me • PM or Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 17th, 2007, 11:45 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7501
Location: Australia
daonlyfreez wrote:
Quote:
is a javascript function. i doubt it is supported by COM the way you are calling it.

Darn
I think ahklerner means "COM_InvokeDeep", which appears to be designed to invoke a string of dot-delimited object properties (not methods.)
Code:
pdoc := COM_Invoke(pweb, "document")
pelem := COM_Invoke(pdoc, "getElementById", "topbanner")
innerHTML := COM_Invoke(pelem, "innerHTML")
COM_Release(pelem)
COM_Release(pdoc)
MsgBox % innerHTML
Untested.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 17th, 2007, 12:26 pm 
Offline

Joined: March 16th, 2005, 10:33 pm
Posts: 968
Location: Frisia
Brilliant!

Thank you very much!

8)

_________________
Image mirror 1mirror 2mirror 3ahk4.me • PM or Image


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 25 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Exabot [Bot], G. Sperotto, Klark92, rbrtryn, tomL and 58 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group