How to pass js variable to imacros variable

Support for iMacros. The iMacros software is the unique solution for automating every activity inside a web browser, for data extraction and web testing.
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
cjseriy
Posts: 16
Joined: Wed Jul 04, 2018 12:53 pm

How to pass js variable to imacros variable

Post by cjseriy » Wed Jul 04, 2018 1:05 pm

VERSION BUILD=11.5.498.2403
Windows 10
Imacros Browser + Firefox Imacros 8.9

Hi!

Could you help me with this please?
I want to pass a variable "varid" from this line to VAR1 and then save it in csv. Is it possible?
The main purpose is to save a js variable to csv and be able to save in the same csv some additional extracted data.

Code: Select all

URL GOTO=javascript:var<SP>varid=document.getElementsByClassName("pull-left")[1].lastElementChild.innerText;
Here is complete code that I am playing with:

Code: Select all

URL GOTO=http://somesite.com/
URL GOTO=javascript:var<SP>varid=document.getElementsByClassName("pull-left")[1].lastElementChild.innerText;
SET !VAR1 {{varid}}
SAVEAS TYPE=EXTRACT FOLDER=* FILE=Extract_{{!NOW:ddmmyy_hh}}.csv
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: How to pass js variable to imacros variable

Post by chivracq » Wed Jul 04, 2018 2:28 pm

cjseriy wrote:

Code: Select all

VERSION BUILD=11.5.498.2403 
Windows 10 
Imacros Browser + Firefox Imacros 8.9
Hi!

Could you help me with this please?
I want to pass a variable "varid" from this line to VAR1 and then save it in csv. Is it possible?
The main purpose is to save a js variable to csv and be able to save in the same csv some additional extracted data.

Code: Select all

URL GOTO=javascript:var<SP>varid=document.getElementsByClassName("pull-left")[1].lastElementChild.innerText;
Here is complete code that I am playing with:

Code: Select all

URL GOTO=http://somesite.com/
URL GOTO=javascript:var<SP>varid=document.getElementsByClassName("pull-left")[1].lastElementChild.innerText;
SET !VAR1 {{varid}}
SAVEAS TYPE=EXTRACT FOLDER=* FILE=Extract_{{!NOW:ddmmyy_hh}}.csv
Euh, yep, what you want is possible, at least in v8.9.7 for FF indeed, I'm not sure if that Syntax/"Trick" is still supported/working in v9.0.3 for FF and v10.0.2 (Beta) for FF, have a look at the following Thread...:
- Re: Getting the Textfield with Focus

The "Principle" is from your 'URL GOTO=javascript' to create/set a 'TITLE' Attribute to some 'HTML' Object, the 'BODY' for example, that will contain the Data/String that you want to pass, and that you then can extract with some 'TAG' + 'EXTRACT=TITLE' Statement... 8)

>>>

Some other Method would be to do some 'EXTRACT=HTM' on some "High Level" HTML Element on the Page like 'HTML' / 'BODY' or some High Level 'DIV' containing all your 'pull-left' Elements (or at least the last one that you are interested in), and using 'EVAL()' to recreate from scratch the same Functionality like your 'lastElementChild.innerText', and this Method would work (and will keep working) in all Browsers/Versions even if 'URL GOTO=javascript' doesn't work or is not supported (anymore)... :idea:

But hum, this is also a bit the same Functionality like the 'SEARCH' Command..., hum except that you want the last Result..., might be a bit cumbersome with 'SEARCH' then indeed, ... if you don't know how many of those 'pull-left' Elements might be present on the Page...

OK, good luck and post your Final Script... :wink:
Last edited by chivracq on Wed Jul 04, 2018 7:07 pm, edited 1 time in total.
- (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...
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: How to pass js variable to imacros variable

Post by chivracq » Wed Jul 04, 2018 2:36 pm

Hum..., and there is probably some "easier" Method as well, using (Negative) 'Relative Positioning' from the Bottom on your Page to directly locate your last 'pull-left' Element to then extract directly with some 'EXTRACT=TXT'... :idea:
- (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...
cjseriy
Posts: 16
Joined: Wed Jul 04, 2018 12:53 pm

Re: How to pass js variable to imacros variable

Post by cjseriy » Wed Jul 04, 2018 6:40 pm

chivracq wrote:Hum..., and there is probably some "easier" Method as well, using (Negative) 'Relative Positioning' from the Bottom on your Page to directly locate your last 'pull-left' Element to then extract directly with some 'EXTRACT=TXT'... :idea:
Thank you very much for your help! Yes I think this would be easier method. I will try to implement it. :D
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: How to pass js variable to imacros variable

Post by chivracq » Wed Jul 04, 2018 7:30 pm

cjseriy wrote:
chivracq wrote:Hum..., and there is probably some "easier" Method as well, using (Negative) 'Relative Positioning' from the Bottom on your Page to directly locate your last 'pull-left' Element to then extract directly with some 'EXTRACT=TXT'... :idea:
Thank you very much for your help! Yes I think this would be easier method. I will try to implement it. :D
OK, shouldn't be very complicated, I would think...

(And I'll wait until you've finished this current Thread..., with your Final Script etc..., before answering your next one...)
- (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...
cjseriy
Posts: 16
Joined: Wed Jul 04, 2018 12:53 pm

Re: How to pass js variable to imacros variable

Post by cjseriy » Wed Jul 04, 2018 9:15 pm

I am doing something wrong. Couldnt extract a value using negative relative position. Honestly the concept behind it is not clear enough for me yet.

Here is the html code

Code: Select all

div class="lower-footer">
                        <div class="pull-left">
                            <span>&copy;  </span>
                            <a href="#"> Terms of Service </a>
                            <a href="#">21</a>
                        </div>
                        <div class="pull-right">
                            <a href="#"><span class="fa fa-facebook"></span></a>
                            <a href="#"><span class="fa fa-twitter"></span></a>
                            <a href="#"><span class="fa fa-linkedin"></span></a>
                            <a href="#"><span class="fa fa-youtube"></span></a>
                            <a href="#"><span class="fa fa-pinterest"></span></a>
                            <a href="#"><span class="fa fa-skype"></span></a>
                        </div>
                    </div>
And I want to extract 21 value but the value is dynamic. So I tried this code:

Code: Select all

TAG POS=2 TYPE=SPAN ATTR=CLASS:fa<SP>fa-facebook
TAG POS=R-1 TYPE=DIV ATTR=CLASS:pull-left EXTRACT=TXT
But it doesnt work for me.
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: How to pass js variable to imacros variable

Post by chivracq » Wed Jul 04, 2018 9:45 pm

cjseriy wrote:I am doing something wrong. Couldnt extract a value using negative relative position. Honestly the concept behind it is not clear enough for me yet.

Here is the html code

Code: Select all

div class="lower-footer">
                        <div class="pull-left">
                            <span>&copy;  </span>
                            <a href="#"> Terms of Service </a>
                            <a href="#">21</a>
                        </div>
                        <div class="pull-right">
                            <a href="#"><span class="fa fa-facebook"></span></a>
                            <a href="#"><span class="fa fa-twitter"></span></a>
                            <a href="#"><span class="fa fa-linkedin"></span></a>
                            <a href="#"><span class="fa fa-youtube"></span></a>
                            <a href="#"><span class="fa fa-pinterest"></span></a>
                            <a href="#"><span class="fa fa-skype"></span></a>
                        </div>
                    </div>
And I want to extract 21 value but the value is dynamic. So I tried this code:

Code: Select all

TAG POS=2 TYPE=SPAN ATTR=CLASS:fa<SP>fa-facebook
TAG POS=R-1 TYPE=DIV ATTR=CLASS:pull-left EXTRACT=TXT
But it doesnt work for me.
Ah OK..., what you want to locate/extract is already "quite" at the bottom of that Page, ah-ah...!

But, Okay-Okay-Okay..., several Solutions, this one for example might be the easiest...:

Code: Select all

TAG POS=1 TYPE=A ATTR=TXT:*Terms<SP>of<SP>Service* EXTRACT=TXT
SET !EXTRACT NULL
TAG POS=R1 TYPE=A ATTR=TXT:* EXTRACT=TXT
PROMPT _{{!EXTRACT}}_
I can't test of course, but I expect your "21" in the '!EXTRACT' / in the 'PROMPT'... See if I'm correct...
- (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...
cjseriy
Posts: 16
Joined: Wed Jul 04, 2018 12:53 pm

Re: How to pass js variable to imacros variable

Post by cjseriy » Thu Jul 05, 2018 10:22 am

Yes, it worked now! Thank you very much :D
I tried the same solution but didn't know what to put in ATTR=TXT:* . I didn't think of putting asterix there.
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: How to pass js variable to imacros variable

Post by chivracq » Thu Jul 05, 2018 12:12 pm

cjseriy wrote:Yes, it worked now! Thank you very much :D
I tried the same solution but didn't know what to put in ATTR=TXT:* . I didn't think of putting asterix there.
OK, good to hear... :D

Yep, you could as well simply have removed the 'TXT' Attr, I would expect "ATTR=" or "ATTR=*" to work as well, but not "ATTR=TXT:" which would expect an empty String.

As you can see, the "Final Solution" is quite far from your original Idea (which would have worked, using the Method I gave you to pass a Var from 'URL GOTO=javascript:' to the main '.iim' Script), it's actually nearly "a pity" you didn't mentioned the URL for this Site, "the Final Solution" is actually just "a Solution", it could have been "funny" to start a "little Game" to find as many Solutions as possible to get the same Result, there are probably more than 20 different Solutions with many Variations, using different Techniques/Methods/Tricks, ah-ah...! :twisted:

>>>

OK, now I'll have a look at your next Thread about 'Fiddler', but hum, I see you haven't corrected the Spelling of "iMacros" in your Sig like I asked you in that other Thread, nor completed the truncated "v8.9" Version, but OK, we'll see in that other Thread... :wink:
- (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...
cjseriy
Posts: 16
Joined: Wed Jul 04, 2018 12:53 pm

Re: How to pass js variable to imacros variable

Post by cjseriy » Thu Jul 05, 2018 2:27 pm

Yes, thank you :) it would be interesting to try a solution with javascript, but currently I will stick with the simplest solution because it just works fine.
Post Reply