| View previous topic :: View next topic |
| Author |
Message |
4birds
Joined: 06 Mar 2008 Posts: 167
|
Posted: Tue Nov 17, 2009 1:39 am Post subject: Need help clicking id element when text can change |
|
|
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 |
|
 |
jethrow
Joined: 24 May 2009 Posts: 1907 Location: Iowa, USA
|
Posted: Tue Nov 17, 2009 1:44 am Post subject: |
|
|
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. _________________
- in case I forgot to smile
Basic Webpage Controls
COM Object Reference |
|
| Back to top |
|
 |
4birds
Joined: 06 Mar 2008 Posts: 167
|
Posted: Tue Nov 17, 2009 1:47 am Post subject: |
|
|
| no it's not. |
|
| Back to top |
|
 |
jethrow
Joined: 24 May 2009 Posts: 1907 Location: Iowa, USA
|
Posted: Tue Nov 17, 2009 1:52 am Post subject: |
|
|
| 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 .
| 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 |
_________________
- in case I forgot to smile
Basic Webpage Controls
COM Object Reference |
|
| Back to top |
|
 |
4birds
Joined: 06 Mar 2008 Posts: 167
|
Posted: Tue Nov 17, 2009 2:32 am Post subject: |
|
|
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
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 |
|
 |
jethrow
Joined: 24 May 2009 Posts: 1907 Location: Iowa, USA
|
Posted: Tue Nov 17, 2009 3:28 am Post subject: |
|
|
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 |
|
_________________
- in case I forgot to smile
Basic Webpage Controls
COM Object Reference |
|
| Back to top |
|
 |
4birds
Joined: 06 Mar 2008 Posts: 167
|
Posted: Tue Nov 17, 2009 4:01 am Post subject: |
|
|
Hahahaaaa! I love AHK! works like a charm, thanks Jethrow!  |
|
| Back to top |
|
 |
jethrow
Joined: 24 May 2009 Posts: 1907 Location: Iowa, USA
|
Posted: Tue Nov 17, 2009 4:03 am Post subject: |
|
|
| Flight4birds wrote: | | ... works like a charm ... |
Which one? I know tank's is much shorter/simpler, but do both examples work? _________________
- in case I forgot to smile
Basic Webpage Controls
COM Object Reference |
|
| Back to top |
|
 |
4birds
Joined: 06 Mar 2008 Posts: 167
|
Posted: Tue Nov 17, 2009 4:16 am Post subject: |
|
|
| 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 |
|
 |
jethrow
Joined: 24 May 2009 Posts: 1907 Location: Iowa, USA
|
Posted: Tue Nov 17, 2009 4:24 am Post subject: |
|
|
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. _________________
- in case I forgot to smile
Basic Webpage Controls
COM Object Reference |
|
| Back to top |
|
 |
4birds
Joined: 06 Mar 2008 Posts: 167
|
Posted: Thu Nov 19, 2009 12:40 am Post subject: |
|
|
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 |
|
 |
4birds
Joined: 06 Mar 2008 Posts: 167
|
Posted: Thu Nov 19, 2009 6:23 pm Post subject: |
|
|
| ping |
|
| Back to top |
|
 |
tank
Joined: 21 Dec 2007 Posts: 3700 Location: Louisville KY USA
|
Posted: Thu Nov 19, 2009 7:05 pm Post subject: |
|
|
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 |
|
 |
4birds
Joined: 06 Mar 2008 Posts: 167
|
Posted: Thu Nov 19, 2009 7:14 pm Post subject: |
|
|
| Yes, the text exist when nothing happens with the 1st code. |
|
| Back to top |
|
 |
tank
Joined: 21 Dec 2007 Posts: 3700 Location: Louisville KY USA
|
Posted: Fri Nov 20, 2009 1:00 am Post subject: |
|
|
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 |
|
 |
|