WatermelonFish wrote: ↑Fri Nov 12, 2021 9:48 pm
Hi all,
Preface:
- I’m a beginner at coding, so your patience is appreciated
- Apologies if this isn’t the right forum group in which to post this question. I wasn’t quite sure where to put it.
- I’ve been looking for the answer to this question for several hours last night and this morning and couldn’t find it. If this type of question is already answered, I apologize for creating a redundant thread and would appreciate the link.
- I tried very hard to include all the information required, as mentioned in the “Please read this before reporting problems” thread. Please go easy on me if I neglected to include some important information. I’ll happily provide anything else that’s needed.
1. What version of iMacros are you using?
'VERSION BUILD=10101485 (FREE Version)
2. What operating system are you using? (please also specify language)
Windows 10 English
3. Which browser(s) are you using? (include version numbers)
Firefox 94.0.1 (64-bit)
4. Do the included demo macros work ok?
Yes
Questions 5-8 are irrelevant, I think
I want my script to be able to check to see if there’s a specific CSS Selector on a webpage. If it doesn’t exist, I want the script to continue. If it does exist, I want the script to extract the HREF/URL link from the selector, open up a new tab, go to the URL extracted from the selector, and then close the tab and return to the first tab.
The CSS selector in question is:
Code: Select all
body > div:nth-child(15) > a:nth-child(1)
I wrote this code out:
Code: Select all
SET !ERRORIGNORE YES
SET !TIMEOUT_STEP 1
TAG SELECTOR="body > div:nth-child(15) > a:nth-child(1)" EXTRACT=HREF
TAB OPEN
TAB T=2
URL GOTO={{!EXTRACT}}
WAIT SECONDS=1
TAB CLOSE
SET !EXTRACT NULL
TAB T=1
The problem with this code is that if the selector doesn’t exist, the script will open a new tab and then freeze (even though I have SET !ERRORIGNORE YES), presumably because it doesn’t know what to do with !EXTRACT, which is NULL. This is why I need the script to be able to check if the selector is on the page (unless there's another solution of which I'm not aware).
Additionally, I’m running the free version of iMacros, which only allows for three custom variables. I’m already using three variables to generate a random number earlier in the script:
Code: Select all
SET !VAR1 1
SET !VAR2 3
SET !VAR3 EVAL("var randomNumber=Math.floor(Math.random()*\"{{!VAR2}}\" +\"{{!VAR1}}\"); randomNumber;")
Is it possible to perform the check for the CSS selector without using any variables? If not, is it possible to generate a random number without the three variables so I can then use them for the check?
Thank you for reading and for your help.
Alright, mini-Compliment on the Good Quality of your OP...
(I'll only move your Thread to the 'Data Extraction' Sub-Forum where it "rather belongs" than the 'General' one, from the Functionality that you need/are using... (But the 'General' Sub-Forum is always "OK" if you are "unsure" where to open your Thread...))
"Simplified" Format you can also use to mention your FCI is:
Code: Select all
iMacros v10.1.0 for FF, 'Free', FF94 (v94.0.1 (64-bit)), Win10_ENG.
>>>
Then alright, to answer your Scenario, hum..., yep indeed, I think the "URL GOTO=#EANF#" when the Link is not found, is "causing" the Pb...
The iMacros v10.x Versions for FF/CR only accept an URL in "http://" or "www", to "prevent" the Use of "Privileged" URL's, or the Script will simply hang... It's supposed to be "by Design", but I consider it a Bug..., or at least "a Bad Design", ah-ah...!
An "easy Workaround" would be, before going to the 'URL GOTO', to check if that Link exists, and using 'EVAL()', to compute the "real" URL, or to "transform" that "#EANF#" into an empty String or some "fake but real" URL, like "
www.google.com" for example, like in:
Code: Select all
SET !ERRORIGNORE YES
SET !TIMEOUT_STEP 1
SET !EXTRACT NULL
TAG SELECTOR="body > div:nth-child(15) > a:nth-child(1)" EXTRACT=HREF
SET !VAR1 EVAL("var s='{{!EXTRACT}}'; var z; if(s!='#EANF#'){z=s;} else{z='';}; z;")
TAB OPEN
TAB T=2
'URL GOTO={{!EXTRACT}}
URL GOTO={{!VAR1}}
WAIT SECONDS=1
TAB CLOSE
TAB T=1
If "your Version" still has Pb with the empty String for the `URL GOTO`, then simply adapt the 'EVAL()' Statement for example to:
Code: Select all
SET !VAR1 EVAL("var s='{{!EXTRACT}}'; var z; if(s!='#EANF#'){z=s;} else{z='www.google.com';}; z;")
And if, when starting your Script, you always make sure you already have some 2nd Tab already open, then you don't need the `TAB OPEN` + `TAB CLOSE` Commands... And that "saves" 2 Lines, from the 50-Line Limitation for the 'Free' Version, ah-ah...!
>>>
And don't worry about the 3 Vars, it's a bit of a "fake" Limitation, easy to bypass, and only meant to nag Users...!
As you can see, I'm using '!VAR1' for the `EVAL()` Statement, to then reuse for the `URL GOTO` further up, but that's "fine", you only use '!VAR1' yourself to compute your Random Nb in '!VAR3', and you can reuse '!VAR1' after that, you don't need it anymore...
For such an "easy" Case, you could even hard-code the "1" + "3" Values in the `EVAL()` like:
Code: Select all
'SET !VAR3 EVAL("var randomNumber=Math.floor(Math.random()*\"{{!VAR2}}\" +\"{{!VAR1}}\"); randomNumber;")
SET !VAR3 EVAL("var randomNumber=Math.floor(Math.random()*3 + 1); randomNumber;")
Or you can also declare "all your Vars" in just 1 Var and use `split()` to access them. Here for example I declare 6 Vars...!:
Code: Select all
SET !VAR1 _1_3_45_99_www.google.com_15_
SET !VAR3 EVAL("var v='{{!VAR1}}'; var x,y,z; x=v.split('_'); z=Math.floor(Math.random()*x[2] + x[1]); z;")
... And 'x[5]' would then be for "
www.google.com"...!
(The Index for `split()` starts at "0", => hence the very first Underscore when declaring the List of Vars in '!VAR1' for the first Var (="1") to be 'x[1]'...)
But..., oh...!, hum...!, mini-Chance the "+ x[1]" in the `EVAL()` might be treated as a String because of the `split()`, and I'm not sure if the `Math.floor()` Method will "automatically" treat or convert it as/to a Number again, => always "safe" is to then "fake" multiply Numbers by 1:
Code: Select all
SET !VAR1 _1_3_45_99_www.google.com_15_
SET !VAR3 EVAL("var v='{{!VAR1}}'; var x,y,z; x=v.split('_'); z=Math.floor(Math.random()*x[2] + x[1]*1); z;")
Not needed for 'x[2]' because that Var is already used for a Multiplication...
- (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...