V7 Relative positioning

From iMacros
Jump to: navigation, search

In order to keep iMacros 7 compatible with iMacros for Firefox as much as possible, we had to change the iMacros relative positioning algorithm. Therefore, it is no longer possible to tag an element relative to its outer element in a nested situation.

This is easier to understand with a simple example. The following macro first tags the table cell TD and looks for the next occurrence of a link.

URL GOTO=http://www.iopus.com/imacros/demo/v6/extract2/
TAG POS=1 TYPE=TD ATTR=CLASS:bdytxt&&TXT:HREF<SP>extracts<SP>links
TAG POS=R1 TYPE=A ATTR=TITLE:* EXTRACT=TXT

While v6 extracts the link which is inside that table cell, namely "HREF extracts links", v7 and iMacros for Firefox extract the next link's text: "keyword detection with EXTRACT".


How to change your macros

Knowing that relative positioning is relative to the element's end, all you have to do is change the anchor (reference) tag. For instance, the v6 macro for extracting the values in each table row:

 VERSION BUILD=6851105 
 TAB T=1
 TAB CLOSEALLOTHERS
 URL GOTO=http://www.x-rates.com/
 'Anchor to the exchange rates table
 TAG POS=2 TYPE=TABLE ATTR=TXT:<SP><SP><SP><SP>USD<SP><SP><SP><SP>GBP*
 'Extract each of the rows *inside* the table
 SET !LOOP 3
 TAG POS=R{{!LOOP}} TYPE=TR ATTR=TXT:* EXTRACT=TXT
 

can be easily adapted to:

 VERSION BUILD=706710521
 TAB T=1
 TAB CLOSEALLOTHERS
 URL GOTO=http://www.x-rates.com/
 'Anchor to the header row
 TAG POS=4 TYPE=TR ATTR=TXT:<SP><SP><SP><SP>USD<SP><SP><SP><SP>GBP*
 'Extract each of the rows *below* the header
 TAG POS=R{{!LOOP}} TYPE=TR ATTR=TXT:* EXTRACT=TXT
 

Notice that both produce the same result, but in the first version !LOOP has to run from 3 to 7, while the new version needs to !LOOP from 1 to 5 to get all the rows in the table.


XPath

Alternatively, you can use XPATH to position the tag. iMacros 7 also supports XPath expressions in the TAG command, although not the id() function.