Problem with selecting a checkbox next to target text (text has no HTML tags)

Discussions and Tech Support related to website data extraction, screen scraping and data mining using iMacros.
Forum rules
iMacros EOL - Attention!

The renewal maintenance has officially ended for Progress iMacros effective November 20, 2023 and all versions of iMacros are now considered EOL (End-of-Life). The iMacros products will no longer be supported by Progress (aside from customer license issues), and these forums will also no longer be moderated from the Progress side.

Thank you again for your business and support.

Sincerely,
The Progress Team

Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information: CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
bigblungus
Posts: 4
Joined: Fri Mar 05, 2021 8:11 pm

Problem with selecting a checkbox next to target text (text has no HTML tags)

Post by bigblungus » Sat Mar 06, 2021 12:13 am

Good evening everyone!

I can't solve a problem with iMacros and it is really bugging me, so I would really appreciate if someone could help me. I hope I've opened the topic on the correct board; otherwise, please forgive me, for this is my first post.
I spent hours and hours going through the wiki and this forum, but I cannot reach my goal of selecting checkboxes with the target text next to them on a specific website. So I'm starting to wonder if it's the problem of the HTML code, or if I'm doing something entirely wrong. I will describe the details below.

1. Config Info:

iMacros for Firefox plugin 8.9.7, free (VERSION BUILD=8970419); browser: Pale Moon 28.9.3; OS: Windows 10 Pro 64-bit English

2. Target site code:

The text content were replaced with general English terms because they were originally in Hungarian. (Also, "etc etc" was written by me in the STYLE section because it was a rather long style section with no real impact on my issue/macro itself.)

Code: Select all

<DIV id="cuccLayer" STYLE="position:absolute; font-weight:bold; (etc etc...); ">
		 <center>Title1</center>
		 <hr>

<input type="checkbox" onClick="cuccArray[0]=1-cuccArray[0];"> 410 pcs. ITEM1<br><input type="checkbox" onClick="cuccArray[1]=1-cuccArray[1];"> 27 pcs. ITEM2<br><input type="checkbox" onClick="cuccArray[2]=1-cuccArray[2];"> 9 pcs. ITEM3<br><input type="checkbox" onClick="cuccArray[3]=1-cuccArray[3];"> 1 pc. ITEM4<br><input type="checkbox" onClick="cuccArray[4]=1-cuccArray[4];"> 89 pcs. ITEM5<br><input type="checkbox" onClick="cuccArray[5]=1-cuccArray[5];"> 23 pcs. ITEM6<br>
		 <hr>
		 <center>
		 <input type="image" src=".../SUBMIT.gif" title="SUBMIT" onClick="document.urlap.par1.value=cuccArrayToString(); document.urlap.Submit.value='svSelejtezes'; document.urlap.submit();">
		 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		 <input type="image" src=".../sample2.gif" width="35" height="35" title="Title2" onClick="cuccHide(); return false;">
		 </center>
</DIV>

3. Problem:

I'm trying to write a macro that will look for specific text(s) on a site and then select the checkbox on the left side of it. For example, let's say I want to select the checkboxes to the left of the texts "pcs.<SP>ITEM3", "pcs.<SP>ITEM4" and "pcs.<SP>ITEM5".

However, the checkboxes and the text are in a shared DIV container called "cuccLayer" and the texts don't have their own HTML tags and because of that, I fail to use relative positioning, even though I went over a lot of chivracq's posts dealing with the topic. I simply cannot come up with a solution, because all the macros I wrote (even after tweaking with POS=R1,R-1, etc) seem to handle all the text as a whole (verified by extraction in test mode) and I cannot position the checkboxes. My last idea before giving up and posting here was using relative positioning with the <br>tag, with no success (maybe TAG cannot be used with empty tags lacking an end tag, or simply <br> is not recognized?)

The position/order of the checkboxes + texts are not static but PHP/JS-related, so the checkbox must be selected based on the current text following it (for more info, see: end of section 4).

4. Context, additional info about the target site and my macro:

A sudden wave of nostalgia hit me, and I started to play again with a 19 year old Hungarian browser-based online RPG which I really enjoyed as a teen. Currently, I'm progressing with the quests. However, because only a few die-hard fans play the game anymore, making money is quite difficult because of a lack of ingame economy, so the only solution left is "farming" gold (meaning doing repetitive tasks to get items that I can sell in the game's own shop itself so I can generate gold without the need to trade with other players on the ingame marketplace).

I already succesfully wrote several "farming" macros that combine both JavaScript and iMacros (this is the main reason I'm using an older build with Pale Moon, due to the need for JS support). This means that the macro automates an ingame activity called mining, and my player character automatically mines ores with various value to sell. However, after a while, my inventory overflows with heavy and cheap ores, taking up much space from the ligher and more expensive ones, such as diamonds etc. When this occurs, a new interface comes in, telling me that I'm carrying too much stuff and I need to throw away something (this currently requires manual intervention on my part, meaning my macro is not fully automated). My target code provided in section 2 is this problematic interface which lists all the amount ("x pcs."), and items ("ITEM3, ITEM4 and ITEM5") in my bag and ask me to select with the checkbox what I want to throw away with a submit form. "ITEM3, ITEM4 and ITEM5" are the heavy and cheap stuff I want to automatically dispose of, so my macro can run in a loop and keep only the expensive items.

Also, because this is a PHP/JS-based online game, the array of items in my inventory are generated by the game and therefore dynamic, so I guess the onClick="cuccArray[x]=1-cuccArray[x]; attribute of the checkbox I want to select cannot really be predicted/automated, because if the items in my inventory change, the array can be numbered completely differently.

5. Closing thoughts

I deliberately chose not to include any of my macros I experimented with, because I tried so many combinations that I wouldn't even know which one to write here. So if this seems as lazy to anyone, please forgive me. Thank you for everyone who went through my post, I would really appreciate some help. Even if my goal cannot be achieved because of the HTML structure, I would like to know it, because otherwise I will keep trying and failing. It might have started out as silly nostalgia but now the idea of me not being to solve the macro is bugging me even more than my love for that old browser RPG. I also really hope that the solution is not something really easy because I would feel very dumb (as I've mentioned, I've already succesfully did scripts and macros for other parts of this game, so I can use iMacros on at least a basic level).
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Problem with selecting a checkbox next to target text (text has no HTML tags)

Post by chivracq » Sat Mar 06, 2021 1:15 am

bigblungus wrote:
Sat Mar 06, 2021 12:13 am
Good evening everyone!

I can't solve a problem with iMacros and it is really bugging me, so I would really appreciate if someone could help me. I hope I've opened the topic on the correct board; otherwise, please forgive me, for this is my first post.
I spent hours and hours going through the wiki and this forum, but I cannot reach my goal of selecting checkboxes with the target text next to them on a specific website. So I'm starting to wonder if it's the problem of the HTML code, or if I'm doing something entirely wrong. I will describe the details below.

1. Config Info:

Code: Select all

iMacros for Firefox plugin 8.9.7, free (VERSION BUILD=8970419); browser: Pale Moon 28.9.3; OS: Windows 10 Pro 64-bit English
2. Target site code:

The text content were replaced with general English terms because they were originally in Hungarian. (Also, "etc etc" was written by me in the STYLE section because it was a rather long style section with no real impact on my issue/macro itself.)

Code: Select all

<DIV id="cuccLayer" STYLE="position:absolute; font-weight:bold; (etc etc...); ">
		 <center>Title1</center>
		 <hr>

<input type="checkbox" onClick="cuccArray[0]=1-cuccArray[0];"> 410 pcs. ITEM1<br><input type="checkbox" onClick="cuccArray[1]=1-cuccArray[1];"> 27 pcs. ITEM2<br><input type="checkbox" onClick="cuccArray[2]=1-cuccArray[2];"> 9 pcs. ITEM3<br><input type="checkbox" onClick="cuccArray[3]=1-cuccArray[3];"> 1 pc. ITEM4<br><input type="checkbox" onClick="cuccArray[4]=1-cuccArray[4];"> 89 pcs. ITEM5<br><input type="checkbox" onClick="cuccArray[5]=1-cuccArray[5];"> 23 pcs. ITEM6<br>
		 <hr>
		 <center>
		 <input type="image" src=".../SUBMIT.gif" title="SUBMIT" onClick="document.urlap.par1.value=cuccArrayToString(); document.urlap.Submit.value='svSelejtezes'; document.urlap.submit();">
		 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		 <input type="image" src=".../sample2.gif" width="35" height="35" title="Title2" onClick="cuccHide(); return false;">
		 </center>
</DIV>

3. Problem:

I'm trying to write a macro that will look for specific text(s) on a site and then select the checkbox on the left side of it. For example, let's say I want to select the checkboxes to the left of the texts "pcs.<SP>ITEM3", "pcs.<SP>ITEM4" and "pcs.<SP>ITEM5".

However, the checkboxes and the text are in a shared DIV container called "cuccLayer" and the texts don't have their own HTML tags and because of that, I fail to use relative positioning, even though I went over a lot of chivracq's posts dealing with the topic. I simply cannot come up with a solution, because all the macros I wrote (even after tweaking with POS=R1,R-1, etc) seem to handle all the text as a whole (verified by extraction in test mode) and I cannot position the checkboxes. My last idea before giving up and posting here was using relative positioning with the <br>tag, with no success (maybe TAG cannot be used with empty tags lacking an end tag, or simply <br> is not recognized?)

The position/order of the checkboxes + texts are not static but PHP/JS-related, so the checkbox must be selected based on the current text following it (for more info, see: end of section 4).

4. Context, additional info about the target site and my macro:

A sudden wave of nostalgia hit me, and I started to play again with a 19 year old Hungarian browser-based online RPG which I really enjoyed as a teen. Currently, I'm progressing with the quests. However, because only a few die-hard fans play the game anymore, making money is quite difficult because of a lack of ingame economy, so the only solution left is "farming" gold (meaning doing repetitive tasks to get items that I can sell in the game's own shop itself so I can generate gold without the need to trade with other players on the ingame marketplace).

I already succesfully wrote several "farming" macros that combine both JavaScript and iMacros (this is the main reason I'm using an older build with Pale Moon, due to the need for JS support). This means that the macro automates an ingame activity called mining, and my player character automatically mines ores with various value to sell. However, after a while, my inventory overflows with heavy and cheap ores, taking up much space from the ligher and more expensive ones, such as diamonds etc. When this occurs, a new interface comes in, telling me that I'm carrying too much stuff and I need to throw away something (this currently requires manual intervention on my part, meaning my macro is not fully automated). My target code provided in section 2 is this problematic interface which lists all the amount ("x pcs."), and items ("ITEM3, ITEM4 and ITEM5") in my bag and ask me to select with the checkbox what I want to throw away with a submit form. "ITEM3, ITEM4 and ITEM5" are the heavy and cheap stuff I want to automatically dispose of, so my macro can run in a loop and keep only the expensive items.

Also, because this is a PHP/JS-based online game, the array of items in my inventory are generated by the game and therefore dynamic, so I guess the onClick="cuccArray[x]=1-cuccArray[x]; attribute of the checkbox I want to select cannot really be predicted/automated, because if the items in my inventory change, the array can be numbered completely differently.

5. Closing thoughts

I deliberately chose not to include any of my macros I experimented with, because I tried so many combinations that I wouldn't even know which one to write here. So if this seems as lazy to anyone, please forgive me. Thank you for everyone who went through my post, I would really appreciate some help. Even if my goal cannot be achieved because of the HTML structure, I would like to know it, because otherwise I will keep trying and failing. It might have started out as silly nostalgia but now the idea of me not being to solve the macro is bugging me even more than my love for that old browser RPG. I also really hope that the solution is not something really easy because I would feel very dumb (as I've mentioned, I've already succesfully did scripts and macros for other parts of this game, so I can use iMacros on at least a basic level).

Alright..., was worth "waiting" for the 2 or 3 hours you took, preparing your OP, ah-ah...! :wink:
=> A big Compliment on the Quality of your Post...! :D

Sorry for "my" big Quote of your OP, but I always quote systematically because "too many" Users delete or mutilate their Posts/Thread once they've got their Answer and Script working, especially when there is "Competition" in the Game... And I "usually" don't help for Games actually, but OK, your Scenario is quite interesting... :twisted:

>>>

Alright, I understand the "Problematic", the CB's corresponding to the x5 'Item_[1-5]' don't have any paired 'LABEL' Element (nor do they have their "separate"/Sub 'DIV') that could be used to tag a specific 'Item_n' CB, then you indeed cannot use 'R-POS' in this Case..., especially also because the Order of the 5 Items/CB's won't be Static.

But..., OK, it's not very difficult to solve, you'll have to "treat" the 5 CB's like if they were 5 "Options" in one DDLB for which you never know the Order but you still want to select an 'Option' by 'Index'... (And the 'Index' will be your 'POS=n' in your Case...) :idea:

And luckily for you, I posted very recently that exact Implementation, applied to a DDLB (that will be your Containing 'DIV' in your Case), and from an 'EXTRACT=TXT' (or '=HTM') (on the 'DIV'), you then can use 'EVAL()' + 'split()' to calculate in which Position 'Item_3' for example is located to reuse that POS_Nb for 'TAG POS=n' on the CB directly, without the Need for 'R-POS'... :idea:

Here is the Thread/Post where I posted that Sol/Script, see if you can come out by yourself, or I'll help you further..., but it's more or less exactly the same Principle like you need...:
- Re: Choose from a drop down - capital specific
(And post your Solution/Final Script anyway if you manage to implement that Func by yourself...)
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
bigblungus
Posts: 4
Joined: Fri Mar 05, 2021 8:11 pm

Re: Problem with selecting a checkbox next to target text (text has no HTML tags)

Post by bigblungus » Sat Mar 06, 2021 7:51 am

Alright..., was worth "waiting" for the 2 or 3 hours you took, preparing your OP, ah-ah...! :wink:
=> A big Compliment on the Quality of your Post...! :D

Sorry for "my" big Quote of your OP, but I always quote systematically because "too many" Users delete or mutilate their Posts/Thread once they've got their Answer and Script working, especially when there is "Competition" in the Game... And I "usually" don't help for Games actually, but OK, your Scenario is quite interesting... :twisted:

>>>

Alright, I understand the "Problematic", the CB's corresponding to the x5 'Item_[1-5]' don't have any paired 'LABEL' Element (nor do they have their "separate"/Sub 'DIV') that could be used to tag a specific 'Item_n' CB, then you indeed cannot use 'R-POS' in this Case..., especially also because the Order of the 5 Items/CB's won't be Static.

But..., OK, it's not very difficult to solve, you'll have to "treat" the 5 CB's like if they were 5 "Options" in one DDLB for which you never know the Order but you still want to select an 'Option' by 'Index'... (And the 'Index' will be your 'POS=n' in your Case...) :idea:

And luckily for you, I posted very recently that exact Implementation, applied to a DDLB (that will be your Containing 'DIV' in your Case), and from an 'EXTRACT=TXT' (or '=HTM') (on the 'DIV'), you then can use 'EVAL()' + 'split()' to calculate in which Position 'Item_3' for example is located to reuse that POS_Nb for 'TAG POS=n' on the CB directly, without the Need for 'R-POS'... :idea:

Here is the Thread/Post where I posted that Sol/Script, see if you can come out by yourself, or I'll help you further..., but it's more or less exactly the same Principle like you need...:
- Re: Choose from a drop down - capital specific
(And post your Solution/Final Script anyway if you manage to implement that Func by yourself...)
Hello chivracq,
thank you for your quick reply. I will answer you properly and address the points you made as soon as possible, but I'm on mobile now and we are visiting relatives for the weekend soon, so it might take a while for me to go over the topic you linked and try to implement it myself. But I will definitely come back with feedback, just a little bit busy these two days, but I won't disappear without a word I promise :) Thank you again.
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Problem with selecting a checkbox next to target text (text has no HTML tags)

Post by chivracq » Sat Mar 06, 2021 10:09 am

bigblungus wrote:
Sat Mar 06, 2021 7:51 am
Hello chivracq,
thank you for your quick reply. I will answer you properly and address the points you made as soon as possible, but I'm on mobile now and we are visiting relatives for the weekend soon, so it might take a while for me to go over the topic you linked and try to implement it myself. But I will definitely come back with feedback, just a little bit busy these two days, but I won't disappear without a word I promise :) Thank you again.

Yep, you're welcome, Thanks for the Follow-up already, and I'm always "Happy" to help Users who put some Effort and Quality in their Thread(s)... :D

Alright, enjoy your WE, and "good Luck" with your Script... Well, you could have taken your Laptop with you for the WE if you get "bored" at some time, then you could have already started to work on your Script, ah-ah...! :twisted: :wink:

(Hum, I once spent Xmas at my Sister's a few years ago, and I was in the middle of a very nice special Xmas "Challenge" in one of the Games I was playing at that time, and I had taken my Laptop with me to keep playing that Game while I was "away", and I needed to regularly "reload" and check my Script, but my Sister and the rest of the Relatives were not "really Happy", ah-ah...! So maybe that's not a "good Idea" after all, ah-ah...! :P )
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
bigblungus
Posts: 4
Joined: Fri Mar 05, 2021 8:11 pm

Re: Problem with selecting a checkbox next to target text (text has no HTML tags)

Post by bigblungus » Sun Mar 07, 2021 10:44 pm

Hello chivracq,

let me reply to your previous 2 posts in 1 reply, for the sake of simplicity... :)
chivracq wrote:
Sat Mar 06, 2021 1:15 am
Alright..., was worth "waiting" for the 2 or 3 hours you took, preparing your OP, ah-ah...! :wink:
=> A big Compliment on the Quality of your Post...! :D

Sorry for "my" big Quote of your OP, but I always quote systematically because "too many" Users delete or mutilate their Posts/Thread once they've got their Answer and Script working, especially when there is "Competition" in the Game... And I "usually" don't help for Games actually, but OK, your Scenario is quite interesting... :twisted:

>>>

Alright, I understand the "Problematic", the CB's corresponding to the x5 'Item_[1-5]' don't have any paired 'LABEL' Element (nor do they have their "separate"/Sub 'DIV') that could be used to tag a specific 'Item_n' CB, then you indeed cannot use 'R-POS' in this Case..., especially also because the Order of the 5 Items/CB's won't be Static.

But..., OK, it's not very difficult to solve, you'll have to "treat" the 5 CB's like if they were 5 "Options" in one DDLB for which you never know the Order but you still want to select an 'Option' by 'Index'... (And the 'Index' will be your 'POS=n' in your Case...) :idea:

And luckily for you, I posted very recently that exact Implementation, applied to a DDLB (that will be your Containing 'DIV' in your Case), and from an 'EXTRACT=TXT' (or '=HTM') (on the 'DIV'), you then can use 'EVAL()' + 'split()' to calculate in which Position 'Item_3' for example is located to reuse that POS_Nb for 'TAG POS=n' on the CB directly, without the Need for 'R-POS'... :idea:

Here is the Thread/Post where I posted that Sol/Script, see if you can come out by yourself, or I'll help you further..., but it's more or less exactly the same Principle like you need...:
- Re: Choose from a drop down - capital specific
(And post your Solution/Final Script anyway if you manage to implement that Func by yourself...)
Thank you for your encouraging words, well, I think if I expect to be helped, I should make some effort with my ask for help... I saw some really poorly written questions in this forum, and I thought this might make my chances better :)

Oh well, if I knew my slow OP-preparing would be visible, I would have written it in a text editor first :oops: I had to take several breaks because of stuff at home, and I didn't want to post a half-arsed thread, haha :lol:

Thanks for making an exception with my question regarding an online game. I usually dislike 'botting' or similar practices in games with an active community, but as I've mentioned, the one I'm playing is quite dead, sadly, so I regard my use of macros only as a 'quality of life' improvement, taken out of necessity...

Thank you very much for pointing me in the right direction. Because of my superficial knowledge of iMacros without some real, diverse practice done, I was thinking inside the box too much... mainly, I was focusing on (double) relative positioning and how to do it without proper anchor tags, but I failed to think for myself about the possibilities of extraction and JS... even though I used extraction methods in my previous macros for this game, but it was only a simple if...else evaluation using EANF...

Anyway, your solution of using EXTRACT=HTM and JS splitting was fantastic... I feel like I made real progress because of it...
chivracq wrote:
Sat Mar 06, 2021 10:09 am

Yep, you're welcome, Thanks for the Follow-up already, and I'm always "Happy" to help Users who put some Effort and Quality in their Thread(s)... :D

Alright, enjoy your WE, and "good Luck" with your Script... Well, you could have taken your Laptop with you for the WE if you get "bored" at some time, then you could have already started to work on your Script, ah-ah...! :twisted: :wink:

(Hum, I once spent Xmas at my Sister's a few years ago, and I was in the middle of a very nice special Xmas "Challenge" in one of the Games I was playing at that time, and I had taken my Laptop with me to keep playing that Game while I was "away", and I needed to regularly "reload" and check my Script, but my Sister and the rest of the Relatives were not "really Happy", ah-ah...! So maybe that's not a "good Idea" after all, ah-ah...! :P )
Ha ha, actually I did just as you wrote, I took my old as heck emergency/'throwaway' laptop with me... 1,86 GHz Intel Pentium, 1 GB RAM, running Linux Lite, a real beast, haha.. :lol: But perfect for playing around with my macro! Luckily, my relatives did not mind me fiddling around, as it was in your case with the Christmas gaming, well maybe it helped that I deceived them and told that I need to do some work related stuff, haha...

Still, I THOUGHT I finished my macro and implemented it in my script, it was not really easy to concentrate with my wife's nephew running and screaming around, haha... and I was just about to post my solution here a few minutes ago, but I've just done just one more test to be 100% sure and there it was... I noticed a serious bug, which manifests itself when any of the 3 items I desire to select are missing... resulting in selecting a completely different item. If all 3 items are present, the macro works perfectly (which was the case in my rushed and distracted testings, so it gave me a false sense of accomplisment).

This is an enormous bug, so I need to come up with a better way to use JS splitting... it would be way more easier if the iMacros syntax allowed if...else conditionals, which I could bypass earlier using the FF plugin's JS environment, but in this case, I don't know yet how to do it efficiently... I'll post an update when I got a solution, right now I'm kinda tired, sorry.
bigblungus
Posts: 4
Joined: Fri Mar 05, 2021 8:11 pm

Re: Problem with selecting a checkbox next to target text (text has no HTML tags)

Post by bigblungus » Tue Mar 09, 2021 12:36 am

6. My solution to my problem in the OP

All right, so this time I think I managed to solve my original problem and testing seems to confirm it, huge thanks again to chivracq for pointing me in the right direction... 8)
Before I post my solution, a little bit of foreword: I have no formal education in JS/coding, and I don't work in the IT area, so I'm totally a hobbyist/enthusiast, and in the case of this problem I was only focused on the solution without giving much care about universal adaptability/code efficiency, therefore it's 100% tailored to my target page's environment and the few target strings it has to work with. I have general ideas about how I might be able to improve it and make it more up to standard, so to say, but at this point I'm happy with myself, almost proud, that I chose the hard way instead of asking for a solution from an expert (whose solution, on the other hand, would have been surely superior to mine, but that's not the point).

Code: Select all

VERSION BUILD=8970419 RECORDER=FX
SET !ERRORIGNORE YES
TAG POS=1 TYPE=DIV ATTR=ID:cuccLayer EXTRACT=HTM
SET throwAway EVAL("var s='{{!EXTRACT}}'; var x,y,z,a,b,c,d,e,f; x='ITEM3'; y=s.includes(x); z=y ? 1 : 0; a=s.split(x); b=a[0].split('<hr>'); c=b[1].split('<input onclick=\"'); d=c[c.length - 1]; e=d.split('\" type='); f=e[-1+z]; f;")
'PROMPT EXTRACT:<SP>_{{!EXTRACT}}_<BR><BR>throwaway:<SP>_{{throwAway}}_
TAG POS=1 TYPE=INPUT ATTR=ONCLICK:{{throwAway}}&&TYPE:checkbox CONTENT=YES
SET throwAway EVAL("var s='{{!EXTRACT}}'; var x,y,z,a,b,c,d,e,f; x='ITEM4'; y=s.includes(x); z=y ? 1 : 0; a=s.split(x); b=a[0].split('<hr>'); c=b[1].split('<input onclick=\"'); d=c[c.length - 1]; e=d.split('\" type='); f=e[-1+z]; f;")
'PROMPT EXTRACT:<SP>_{{!EXTRACT}}_<BR><BR>throwaway:<SP>_{{throwAway}}_
TAG POS=1 TYPE=INPUT ATTR=ONCLICK:{{throwAway}}&&TYPE:checkbox CONTENT=YES
SET throwAway EVAL("var s='{{!EXTRACT}}'; var x,y,z,a,b,c,d,e,f; x='ITEM5'; y=s.includes(x); z=y ? 1 : 0; a=s.split(x); b=a[0].split('<hr>'); c=b[1].split('<input onclick=\"'); d=c[c.length - 1]; e=d.split('\" type='); f=e[-1+z]; f;")
'PROMPT EXTRACT:<SP>_{{!EXTRACT}}_<BR><BR>throwaway:<SP>_{{throwAway}}_
TAG POS=1 TYPE=INPUT ATTR=ONCLICK:{{throwAway}}&&TYPE:checkbox CONTENT=YES
TAG POS=1 TYPE=INPUT:IMAGE ATTR=SRC:../123.gif
As I've mentioned in my previous reply, my former solution (which turned out to be buggy, and therefore bad :oops: ), selected the last checkbox even when the target string was not present, due to the unsophisticated way of my .splitting methods (by the way, I suppose there are better solutions than splitting, but as I've written earlier, I focused on the results for this particular target page and not code efficiency). This was the code:

'BAD CODE!
TAG POS=1 TYPE=DIV ATTR=ID:cuccLayer EXTRACT=HTM
SET throwaway EVAL("var s='{{!EXTRACT}}'; var a,b,c,d,e,f; a=s.split('ITEM3'); b=a[0].split('<hr>'); c=b[1].split('<input onclick=\"'); d=c[c.length - 1]; e=d.split('\" type='); f=e[0]; f;")


And because I couldn't use an else...if conditional in a .iim macro, I had to add a way of preventing the code from splitting the DIV even when the target strings are not present, so I added the x, y, z variables to check for a truth condition, convert the boolean to either 0 or 1, and add the resulting number to the array index of -1 in the f variable. When the target strings are not present, it leads to z being 0, and therefore the negative array index of e in the variable f leads to an error (which errors are ignored due to SET !ERRORIGNORE YES ), therefore no checkbox will be selected because it leads to an error in the checkbox attribute.

So, this is my crude, obsolete, verbose solution to the problem, but hey, "if it works it ain't stupid"! :lol:
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Problem with selecting a checkbox next to target text (text has no HTML tags)

Post by chivracq » Tue Mar 09, 2021 9:57 am

Alright, I "only" quote this last Reply as it contains your Script...:
bigblungus wrote:
Tue Mar 09, 2021 12:36 am
6. My solution to my problem in the OP

All right, so this time I think I managed to solve my original problem and testing seems to confirm it, huge thanks again to chivracq for pointing me in the right direction... 8)
Before I post my solution, a little bit of foreword: I have no formal education in JS/coding, and I don't work in the IT area, so I'm totally a hobbyist/enthusiast, and in the case of this problem I was only focused on the solution without giving much care about universal adaptability/code efficiency, therefore it's 100% tailored to my target page's environment and the few target strings it has to work with. I have general ideas about how I might be able to improve it and make it more up to standard, so to say, but at this point I'm happy with myself, almost proud, that I chose the hard way instead of asking for a solution from an expert (whose solution, on the other hand, would have been surely superior to mine, but that's not the point).

Code: Select all

VERSION BUILD=8970419 RECORDER=FX
SET !ERRORIGNORE YES
TAG POS=1 TYPE=DIV ATTR=ID:cuccLayer EXTRACT=HTM
SET throwAway EVAL("var s='{{!EXTRACT}}'; var x,y,z,a,b,c,d,e,f; x='ITEM3'; y=s.includes(x); z=y ? 1 : 0; a=s.split(x); b=a[0].split('<hr>'); c=b[1].split('<input onclick=\"'); d=c[c.length - 1]; e=d.split('\" type='); f=e[-1+z]; f;")
'PROMPT EXTRACT:<SP>_{{!EXTRACT}}_<BR><BR>throwaway:<SP>_{{throwAway}}_
TAG POS=1 TYPE=INPUT ATTR=ONCLICK:{{throwAway}}&&TYPE:checkbox CONTENT=YES
SET throwAway EVAL("var s='{{!EXTRACT}}'; var x,y,z,a,b,c,d,e,f; x='ITEM4'; y=s.includes(x); z=y ? 1 : 0; a=s.split(x); b=a[0].split('<hr>'); c=b[1].split('<input onclick=\"'); d=c[c.length - 1]; e=d.split('\" type='); f=e[-1+z]; f;")
'PROMPT EXTRACT:<SP>_{{!EXTRACT}}_<BR><BR>throwaway:<SP>_{{throwAway}}_
TAG POS=1 TYPE=INPUT ATTR=ONCLICK:{{throwAway}}&&TYPE:checkbox CONTENT=YES
SET throwAway EVAL("var s='{{!EXTRACT}}'; var x,y,z,a,b,c,d,e,f; x='ITEM5'; y=s.includes(x); z=y ? 1 : 0; a=s.split(x); b=a[0].split('<hr>'); c=b[1].split('<input onclick=\"'); d=c[c.length - 1]; e=d.split('\" type='); f=e[-1+z]; f;")
'PROMPT EXTRACT:<SP>_{{!EXTRACT}}_<BR><BR>throwaway:<SP>_{{throwAway}}_
TAG POS=1 TYPE=INPUT ATTR=ONCLICK:{{throwAway}}&&TYPE:checkbox CONTENT=YES
TAG POS=1 TYPE=INPUT:IMAGE ATTR=SRC:../123.gif
As I've mentioned in my previous reply, my former solution (which turned out to be buggy, and therefore bad :oops: ), selected the last checkbox even when the target string was not present, due to the unsophisticated way of my .splitting methods (by the way, I suppose there are better solutions than splitting, but as I've written earlier, I focused on the results for this particular target page and not code efficiency). This was the code:

'BAD CODE!
TAG POS=1 TYPE=DIV ATTR=ID:cuccLayer EXTRACT=HTM
SET throwaway EVAL("var s='{{!EXTRACT}}'; var a,b,c,d,e,f; a=s.split('ITEM3'); b=a[0].split('<hr>'); c=b[1].split('<input onclick=\"'); d=c[c.length - 1]; e=d.split('\" type='); f=e[0]; f;")


And because I couldn't use an else...if conditional in a .iim macro, I had to add a way of preventing the code from splitting the DIV even when the target strings are not present, so I added the x, y, z variables to check for a truth condition, convert the boolean to either 0 or 1, and add the resulting number to the array index of -1 in the f variable. When the target strings are not present, it leads to z being 0, and therefore the negative array index of e in the variable f leads to an error (which errors are ignored due to SET !ERRORIGNORE YES ), therefore no checkbox will be selected because it leads to an error in the checkbox attribute.

So, this is my crude, obsolete, verbose solution to the problem, but hey, "if it works it ain't stupid"! :lol:

Yes-yes-yes...!, Excellent...!! :)

I'm completely Impressed, ah-ah...! :D 8) :wink:

And you "neatly" used the Syntax I've been "promoting" for 'EVAL()' for several years, in a perfect "Demonstration" that this Syntax is "indeed" easy to reuse and debug for complex Expressions, even for Newbies and "Not-Programmers"...! :idea:

Then..., yep-yep, simply Perfect..., even if "of course", I would have some Remarks, ah-ah...!
Then OK, I understand why your first Attempt was not completely working, because you went for isolating and reconstructing the whole 'onClick' Attribute from the "<input type="checkbox" onClick="cuccArray[0]=1-cuccArray[0];"> 410 pcs. ITEM1<br>".

Hum, OK, why not..., that's a valid Approach, even if I would "only" have gone for the changing Nb in "[n]" instead of reconstructing the whole String, and I (fore)see a "Danger", because in the 'EXTRACT=HTM', it looks like the Order of the 'type' + 'onClick' Attributes have permuted, from the Source Extract you had posted in your OP, meaning there is a Chance your Code might not always work, oops...! :P
Well, the 'TAG' Statements won't do/select anything in that Case when/if that happens...

Here is then I think a more reliable Approach, and a bit easier/simpler Implementation...:
(I write on the fly by editing a Copy&Paste of your Code...)

Code: Select all

VERSION BUILD=8970419 RECORDER=FX
SET !ERRORIGNORE YES
SET !EXTRACT_TEST_POPUP NO
SET !TIMEOUT_STEP 0

'Easy Access Vars:
'***********************
'Throwaway Items:
SET ThA_Item_1 "Item_I with its real Name...!"
SET ThA_Item_2 "Item_2 with its real Name...!"
SET ThA_Item_3 "Item_3 with its real Name...!"

'Extract 'DIV' containing all CB's:
SET !EXTRACT NULL
TAG POS=1 TYPE=DIV ATTR=ID:cuccLayer EXTRACT=HTM

'Compute POS_Nb for CB's to select (or =0 if Item is not present):
'Item_1:
SET Item {{ThA_Item_1}}
SET POS_Item_1 EVAL("var s='{{!EXTRACT}}', it='{{Item}}'; var x,y,z; x=s.split(it); y=x[0].split('pc'); z=(y.length - 1); z;")
'>
'Item_2:
SET Item {{ThA_Item_2}}
SET POS_Item_2 EVAL("var s='{{!EXTRACT}}', it='{{Item}}'; var x,y,z; x=s.split(it); y=x[0].split('pc'); z=(y.length - 1); z;")
'>
'Item_3:
SET Item {{ThA_Item_3}}
SET POS_Item_3 EVAL("var s='{{!EXTRACT}}', it='{{Item}}'; var x,y,z; x=s.split(it); y=x[0].split('pc'); z=(y.length - 1); z;")

'Debug:
SET Debug_Msg EXTRACT:<BR>_{{!EXTRACT}}_<BR><BR>Item_1:<SP>_{{ThA_Item_1}}_<BR>POS_Item_1:<SP>_{{POS_Item_1}}_
ADD Debug_Msg <BR><BR>Item_2:<SP>_{{ThA_Item_2}}_<BR>POS_Item_2:<SP>_{{POS_Item_2}}_
ADD Debug_Msg <BR><BR>Item_3:<SP>_{{ThA_Item_3}}_<BR>POS_Item_3:<SP>_{{POS_Item_3}}_
'>
PROMPT {{Debug_Msg}}
PAUSE

'Throw away Items:
TAG POS={{POS_Item_1}} TYPE=INPUT ATTR=TYPE:checkbox&&ONCLICK:cuccArray* CONTENT=YES
TAG POS={{POS_Item_2}} TYPE=INPUT ATTR=TYPE:checkbox&&ONCLICK:cuccArray* CONTENT=YES
TAG POS={{POS_Item_3}} TYPE=INPUT ATTR=TYPE:checkbox&&ONCLICK:cuccArray* CONTENT=YES
'>
TAG POS=1 TYPE=INPUT:IMAGE ATTR=SRC:../123.gif
(Not tested of course...)

I would also expect this Implementation to work on 'EXTRACT=TXT' on the Containing 'DIV' as well...! 8)

And as you can see, this Implementation doesn't even use any if/else, I let 'split()' do all the Conditional Logic by itself..., by simply counting the Nb of "pcs" Occurrences before the Name of the specified Item... => Simplicity is always "Best", ah-ah...! :wink:

>>>

EDIT_1: :oops:
Oh wait...!, I was wondering indeed if you always get the "s" in "pcs", and your Source Extract in your OP gives the Answer in "<input type="checkbox" onClick="cuccArray[3]=1-cuccArray[3];"> 1 pc. ITEM4<br>", => "pcs" that I used for the 2nd 'split()' will be Buggy when there is/are any Item(s) of which you only have 1, ah-ah...! => Need to use "pc" instead, hoping those 2 Letters are only used in "pc." and "pcs." and not in any Item Name(s)...
I've adapted the Script accordingly... :P

EDIT_2:
And hum..., my Implementation "takes for granted" that when an Item is not present, that it is "really" not present in the 'DIV'...
If that Item still gets displayed as "[...] 0 pc. ITEM4<br>" for example, then hum..., my Script will be Buggy as 'POS_Item_n' will then select the "next" CB in the List..., unless the CB is still present but greyed out/disabled, then the Count might still be correct... (if the 'onClick' ATTR is still correct)... But I can't tell as you didn't post the Source for that Case... :?
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
Post Reply