AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Need help clicking id element when text can change
Goto page 1, 2  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
4birds



Joined: 06 Mar 2008
Posts: 167

PostPosted: Tue Nov 17, 2009 1:39 am    Post subject: Need help clicking id element when text can change Reply with quote

I am using COM to click an element on a web page.

Currently, here is my code
Code:
#Include C:\Program Files\AutoHotkey\LIB\COM.ahk
COM_Init()
F1::
{
pwb:=iWeb_getwin("External Launch")
com_invoke(pwb,"document.all[8].contentwindow.document.all[15].contentwindow.document.all[img-665580-665580-3899355].click")
}
return


My problem is that the numbers "665580-665580" can frequently change while the number "3899355" remains static. Beacause the 1st numbers change the script will be unable to find the element. The script must "see" the number 3899355 but be unable to ignore the others. How can I write the script as to remedy this problem?
Back to top
View user's profile Send private message
jethrow



Joined: 24 May 2009
Posts: 1907
Location: Iowa, USA

PostPosted: Tue Nov 17, 2009 1:44 am    Post subject: Reply with quote

Is it the first element on the page with that sort of name/id? If so, you could ReExMatch based one the entire frame HTML to find the numbers.
_________________
Very Happy - in case I forgot to smile
Basic Webpage Controls
COM Object Reference
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
4birds



Joined: 06 Mar 2008
Posts: 167

PostPosted: Tue Nov 17, 2009 1:47 am    Post subject: Reply with quote

no it's not.
Back to top
View user's profile Send private message
jethrow



Joined: 24 May 2009
Posts: 1907
Location: Iowa, USA

PostPosted: Tue Nov 17, 2009 1:52 am    Post subject: Reply with quote

Flight4birds wrote:
no it's not.
Hmmm, well something like this might still work:
Code:
frame := COM_Invoke(pwb, "document.all[8].contentwindow.document.all[15].contentwindow")
HTML := COM_Invoke(frame, "document.documentElement.innerHTML") ; get frame HTML
RegExMatch(HTML,"img-\d+-\d+-3899355",id)
COM_Invoke(frame,"document.all[" id "].click")
COM_Release(frame)

Otherwise, I would try to find another way to identify the element (value, innerText, innerHTML, href).


Also, I would recommend releasing all COM objects and Terminating COM if you post a fully functional script Wink .
Code:
COM_Init()
F1::
{
pwb:=iWeb_getwin("External Launch")
com_invoke(pwb,"document.all[8].contentwindow.document.all[15].contentwindow.document.all[img-665580-665580-3899355].click")
COM_Release(pwb), COM_Term()
}
return

_________________
Very Happy - in case I forgot to smile
Basic Webpage Controls
COM Object Reference
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
4birds



Joined: 06 Mar 2008
Posts: 167

PostPosted: Tue Nov 17, 2009 2:32 am    Post subject: Reply with quote

jethrow said
Quote:
Otherwise, I would try to find another way to identify the element (value, innerText, innerHTML, href).


Can we try the innertext route?
Using tank's iwebbrowser2 window spy I get
Code:
MX55/29 Nov

as the inner text of the element.
What's the code to click this element?
Also, because of the quirkiness of this web page, can we implement the code I used at the top? It's the only way I've gotten COM to work on this web page. thanks.
Back to top
View user's profile Send private message
jethrow



Joined: 24 May 2009
Posts: 1907
Location: Iowa, USA

PostPosted: Tue Nov 17, 2009 3:28 am    Post subject: Reply with quote

Try this (note - the javascript is case-sensitive):
Code:
#Include C:\Program Files\AutoHotkey\LIB\COM.ahk
COM_Init()
innerText := "MX55/29 Nov"

F1::
   pwb:=iWeb_getwin("External Launch")
   code := "javascript: all=document.all[8].contentWindow.document.all[15].contentWindow.document.all; for(i=0;i<all.length;i++){if(all[i].innerText=='" innerText "'){break}}; void 0"
   COM_Invoke(pwb, "Navigate", code)
   index := COM_Invoke(pwb, "document.parentWindow.i")
   COM_Invoke(pwb,"document.all[8].contentwindow.document.all[15].contentwindow.document.all[" index "].click")
   COM_Release(pwb), COM_Term()
Return


Note - I believe the updated iWeb function library that tank & sinkfaze are working on should be able to recurse into frames and click on an element by innerText:
tank wrote:
Code:
F1::
COM_Init()
   pwb:=iWeb_getwin("External Launch")
   iWeb_clickText(pwb,"MX55/29 Nov","8,15")
   COM_Release(pwb), COM_Term()
Return

_________________
Very Happy - in case I forgot to smile
Basic Webpage Controls
COM Object Reference
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
4birds



Joined: 06 Mar 2008
Posts: 167

PostPosted: Tue Nov 17, 2009 4:01 am    Post subject: Reply with quote

Hahahaaaa! I love AHK! works like a charm, thanks Jethrow! Laughing
Back to top
View user's profile Send private message
jethrow



Joined: 24 May 2009
Posts: 1907
Location: Iowa, USA

PostPosted: Tue Nov 17, 2009 4:03 am    Post subject: Reply with quote

Flight4birds wrote:
... works like a charm ...

Which one? I know tank's is much shorter/simpler, but do both examples work?
_________________
Very Happy - in case I forgot to smile
Basic Webpage Controls
COM Object Reference
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
4birds



Joined: 06 Mar 2008
Posts: 167

PostPosted: Tue Nov 17, 2009 4:16 am    Post subject: Reply with quote

whoops sorry. I got the first one to work, the second code has an error message at line 4 saying that there are too many parameters passed to that function.
Back to top
View user's profile Send private message
jethrow



Joined: 24 May 2009
Posts: 1907
Location: Iowa, USA

PostPosted: Tue Nov 17, 2009 4:24 am    Post subject: Reply with quote

You need to download the updated iWeb Standard Library.

Note - I'm pretty sure tank will be changing it, but currently the top part of the library (above iWeb_Init()) needs to be commented-out.
_________________
Very Happy - in case I forgot to smile
Basic Webpage Controls
COM Object Reference
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
4birds



Joined: 06 Mar 2008
Posts: 167

PostPosted: Thu Nov 19, 2009 12:40 am    Post subject: Reply with quote

I updated the new iWeb Standard Library but when I run the code
Code:
F1::
COM_Init()
   pwb:=iWeb_getwin("External Launch")
   iWeb_clickText(pwb,"MX55/29 Nov","8,15")
   COM_Release(pwb), COM_Term()
Return


nothing happens, no error message or anything.

I have another question though about the code that is working here
Code:
#Include C:\Program Files\AutoHotkey\LIB\COM.ahk
COM_Init()
innerText := "MX55/29 Nov"

F1::
   pwb:=iWeb_getwin("External Launch")
   code := "javascript: all=document.all[8].contentWindow.document.all[15].contentWindow.document.all; for(i=0;i<all.length;i++){if(all[i].innerText=='" innerText "'){break}}; void 0"
   COM_Invoke(pwb, "Navigate", code)
   index := COM_Invoke(pwb, "document.parentWindow.i")
   COM_Invoke(pwb,"document.all[8].contentwindow.document.all[15].contentwindow.document.all[" index "].click")
   COM_Release(pwb), COM_Term()
Return

When the text does exist, the code works as it should and clicks the text. When the text is not there I get an error message about click not being a proper dispatch object. What I'd like to do is eventually run an loop script so that when the text "MX55/29 Nov" does exist, it is clicked.But with the error message popping up ever time it doesn't find the text, this is problematic. Any thoughts or pointers?

Also, is it possible, using this script to search for a whole set of text? For ex. if any of the text appears (MX55/29 Nov, FD42/28 Nov, GW88/26 Nov" it will click whichever one comes appears first.
Thanks for the help.
Back to top
View user's profile Send private message
4birds



Joined: 06 Mar 2008
Posts: 167

PostPosted: Thu Nov 19, 2009 6:23 pm    Post subject: Reply with quote

ping
Back to top
View user's profile Send private message
tank



Joined: 21 Dec 2007
Posts: 3700
Location: Louisville KY USA

PostPosted: Thu Nov 19, 2009 7:05 pm    Post subject: Reply with quote

does the text exist when nothing happens?


the second one gives you an error because it attempts to click an element that doesnt exist

the iweb function just does nothing if the text isnt found
_________________

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;
Back to top
View user's profile Send private message
4birds



Joined: 06 Mar 2008
Posts: 167

PostPosted: Thu Nov 19, 2009 7:14 pm    Post subject: Reply with quote

Yes, the text exist when nothing happens with the 1st code.
Back to top
View user's profile Send private message
tank



Joined: 21 Dec 2007
Posts: 3700
Location: Louisville KY USA

PostPosted: Fri Nov 20, 2009 1:00 am    Post subject: Reply with quote

and this text(MX55/29 Nov) appears nested somewhere inside an A tag?
_________________

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;
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group