Extract Number of Elements

Discussions and Tech Support related to the iMacros Firefox Add-on, including the built-in Javascript scripting interface for playing .js files.
Forum rules
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
Inflicted
Posts: 1
Joined: Wed Jul 20, 2016 1:16 pm

Extract Number of Elements

Post by Inflicted » Wed Jul 20, 2016 1:27 pm

Hello Sirs and Madams

I was wondering how I would be able to grab the total amount of elements within a specific div in a dynamic environment. Specifically with regards to an eCommerce store that would change products on a daily basis.

How would I go about grabbing the total amount of products which are available?

I would like to store that value in a variable for later manipulation.

For example, there are 10 products available on that page (10 links), how would I manage to extract the total (10) and use that.

From what I've searched for, this might be easily accomplished via the EXTRACT, EVAL and/or TAG functions, however I am having some difficulty with the matter.

Would it be easier to do this with JavaScript instead?

EDIT:
VERSION BUILD=8970419 RECORDER=FX
OS: Ubuntu 14.04.4 LTS Release: 14.04
Browser: Firefox 47

With some searching I found this:
http://forum.imacros.net/viewtopic.php?f=11&t=26030
Shows that you can play .iim files in a .js file with the following command:
URL GOTO=imacros://run/?m=your-javascript.js

Would using something like this be advised:
$("#mylist li").length

Apologies, I am not knowledgeable on the topic, all suggestions are welcome.

Any assistance on the matter would be greatly appreciated. Many thanks for your time.
chivracq
Posts: 8720
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Extract Number of Elements

Post by chivracq » Wed Jul 20, 2016 2:28 pm

Inflicted wrote:Hello Sirs and Madams

I was wondering how I would be able to grab the total amount of elements within a specific div in a dynamic environment. Specifically with regards to an eCommerce store that would change products on a daily basis.

How would I go about grabbing the total amount of products which are available?

I would like to store that value in a variable for later manipulation.

For example, there are 10 products available on that page (10 links), how would I manage to extract the total (10) and use that.

From what I've searched for, this might be easily accomplished via the EXTRACT, EVAL and/or TAG functions, however I am having some difficulty with the matter.

Would it be easier to do this with JavaScript instead?

EDIT:

Code: Select all

VERSION BUILD=8970419 RECORDER=FX
OS: Ubuntu 14.04.4 LTS Release: 14.04
Browser: Firefox 47
With some searching I found this:
http://forum.imacros.net/viewtopic.php?f=11&t=26030
Shows that you can play .iim files in a .js file with the following command:
URL GOTO=imacros://run/?m=your-javascript.js

Would using something like this be advised:
$("#mylist li").length

Apologies, I am not knowledgeable on the topic, all suggestions are welcome.

Any assistance on the matter would be greatly appreciated. Many thanks for your time.
CIM...! :mrgreen: (Always mention your FCI when you open a Thread, read my Sig, many Commands are not available for all Browsers/Version...)

EDIT: Hum, OK, you keep editing your OP while I'm already responding, FCI mentioned... Good...! :D

=> The "URL GOTO=imacros://run/?m=your-javascript.js" Synatax for example only works on FF...

Your "$("#mylist li").length" is a good idea, I've posted already several Examples on the Forum where I use a similar Technique consisting of doing an 'EXTRACT=HTM' at your 'DIV' Level, which will contain all the Links, and then Length (-1) of the Array returned by 'split()' will give you the Number of Occurrences of the Keyword you used in the 'split()'.
Here is an Example (on the Nb of Select Options in a DDLB, but the Technique is the same for any other Element):
- Re: Number of Options in a Select tag
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
iimfun
Posts: 239
Joined: Tue Jul 19, 2016 1:06 pm

Re: Extract Number of Elements

Post by iimfun » Thu Jul 21, 2016 7:35 am

Here is a possible solution if you're applying a pure iim macro

Code: Select all

' specify your selector here
SET selectScr "var num = document.querySelectorAll('div').length;"

SET !POPUP_ALLOWED {{!URLCURRENT}}
SET openScr "eval('window.open(\\"javascript:'+num+'\\")');"
URL GOTO=javascript:(function(){<SP>{{selectScr}}{{openScr}}<SP>})();
WAIT SECONDS=0.1
SET !EXTRACT_TEST_POPUP NO
TAG POS=1 TYPE=* ATTR=* EXTRACT=TXT
TAB CLOSE

SET number {{!EXTRACT}}
'PROMPT {{number}}
Post Reply