Javascript functions not working

Discussions and Tech Support related to automating the iMacros Browser or Internet Explorer from any scripting and programming language, such as VBS (WSH), VBA, VB, Perl, Delphi, C# or C++.
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
sid78669
Posts: 3
Joined: Sat Sep 10, 2011 11:21 pm

Javascript functions not working

Post by sid78669 » Sat Sep 10, 2011 11:28 pm

I am new to the javascript scripting for iMacro so please bear with me. I'm trying to automate firefox to open a page and then open the 2nd last link. However, to get the second last link, I'm using document.getElementsByTagName. This is throwing this error:

Code: Select all

TypeError: document.getElementsByTagName("form")[0] is undefined (Error code: 991)

The Code is:

Code: Select all

var reportDataTable = document.getElementsByTagName("form")[0].getElementsByTagName("table")[0];
var reportCount = reportDataTable.getElementsByTagName("tr").length - 2;
var reportLinks = reportDataTable.getElementsByTagName("a");
alert("Total Reports: " + reportCount + "Total Report Links: " + reportLinks.length);
/*var links = "Links: \n";
for(i = 0; i < reportLinks.length; i++)
	links = links + reportLinks[i].innerHTML + "\n";	
alert(links);*/
reportLinks[reportLinks.length - 2].click();
for(i = 0; i < reportCount; i++)
{
	iimDisplay("Loop: " + i);
	iimPlay("Reports.iim");
}
Now, I use firebug 1.8.2 for my javascript debugging, and if I execute the script from its console, everything works (- the iMacro part).

Tech-Specs
iMacro Version: VERSION BUILD=7300701 RECORDER=FX
OS: Win 7 64-Bit
Browser: Firefox 6.0.2
Every macro including the Reports.iim used here is working.

Any suggestions?
Tom, Tech Support
Posts: 3572
Joined: Mon May 31, 2010 4:59 pm

Re: Javascript functions not working

Post by Tom, Tech Support » Mon Sep 12, 2011 12:10 pm

Hi sid78669,

I'm finding conflicting information on whether or not it is possible to access the web page DOM from within the iMacros Javascript interface. For example, see this post.

I will have to research this further and get back to you.

But in the meantime, rather than trying to use Javascript to access the DOM to click the second-to-last link, why not use a macro to just do the same?
Regards,

Tom, iMacros Support
sid78669
Posts: 3
Joined: Sat Sep 10, 2011 11:21 pm

Re: Javascript functions not working

Post by sid78669 » Mon Sep 12, 2011 10:17 pm

Thanks for the reply Tom. The reason for accessing DOM instead of using a simple macro is that the report's name and link are dynamic and are hence changing all the time. Also, the number of reports are never the same, you can have anywhere from 1 to 100 reports. Hence, I need to select the last report and the javascript method is the most suitable.
Tom, Tech Support
Posts: 3572
Joined: Mon May 31, 2010 4:59 pm

Re: Javascript functions not working

Post by Tom, Tech Support » Tue Sep 13, 2011 7:59 am

Hi sid,

I agree it may be a bit more straightforward to do this using the DOM (especially if you are already familiar and comfortable with that model), but it should still be possible to cilck the desired the link using a macro, especially if you employ wildcards to account for dynamic attributes, and relative positioning to target the correct link.
Regards,

Tom, iMacros Support
sid78669
Posts: 3
Joined: Sat Sep 10, 2011 11:21 pm

Re: Javascript functions not working

Post by sid78669 » Tue Sep 13, 2011 12:57 pm

I like the idea of using wildcards. However, as I mentioned about the dynamic number of reports, the list can be long,making it required for me to scroll down a bit. And the reports do have a similar name followed by the ID.

I'll look into a way to try and do it dynamically and will try to keep you posted.
Tom, Tech Support
Posts: 3572
Joined: Mon May 31, 2010 4:59 pm

Re: Javascript functions not working

Post by Tom, Tech Support » Tue Sep 13, 2011 1:52 pm

Sid,

Stay tuned, I got some feedback from one of the developers and will follow up shortly...
Regards,

Tom, iMacros Support
Tom, Tech Support
Posts: 3572
Joined: Mon May 31, 2010 4:59 pm

Re: Javascript functions not working

Post by Tom, Tech Support » Tue Sep 13, 2011 1:58 pm

Change the first line in your code to the following and it should work:

Code: Select all

var reportDataTable = window.content.document.getElementsByTagName("form")[0].getElementsByTagName("table")[0];
In order to access the web page DOM, you need to prefix your reference to the document object with window.content.
Regards,

Tom, iMacros Support
anodynos
Posts: 1
Joined: Mon Oct 21, 2013 4:08 pm

Re: Javascript functions not working

Post by anodynos » Mon Oct 21, 2013 4:17 pm

Can I access window and its variables from iMacros ?

With

Code: Select all

window.console.log('window', window); 
I get |"window" [object Window]| on the console, as expected.

But issuing

Code: Select all

window.console.log('$', window.$);
I get |"$" undefined|, although window.$ is available on the page and I can access it from firebug/firefox Developer tools.

I am using the latest version of FIrefox 24 & iMacros for Firefox 8.5.1 & I also have iMacros 9 Trial (full) on my machine. This feature is extremely important for me and my client.
Tom, Tech Support
Posts: 3572
Joined: Mon May 31, 2010 4:59 pm

Re: Javascript functions not working

Post by Tom, Tech Support » Wed Oct 23, 2013 1:53 pm

Hi anodynos,

Sorry, for various design and security reasons, it is not likely that we would make arbitrary properties/methods of the window object accessible via iMacros. In fact, there was even considerable debate as to whether to continue exposing the DOM via an iMacros .js script!
Regards,

Tom, iMacros Support
Post Reply