Solved

Windows Script Host.  Automate IE to click on a link.

Posted on 2009-04-08
9
3,697 Views
Last Modified: 2013-12-08
I'm trying to automate some of my daily routine and came across "Windows Script Host".  I'm very new to wsh and I do not know its limits.  At the moment I have code that writes to a text box both user name and password then click the 'ok' to submit.  

The next step is, the page that appears after logging in I would like to trigger a click event on a hyperlink.  I've been at this for a day or two but I do not seem to be able to find a clear cut answer.
I'm going to leave a snippet of the HTML code that I want 'clicked' and also the code that I already have so far.  Please, show me how to click a hyper link using wsh.  (I would perfer javascript but vbscript is acceptable)(i'm using a windows 2003 box)
<A class="SIMainTabText" href="" onClick="onTabClick('reports',getForm(this)); return false;">Inv Sys Requests</a> 
 
ie = WScript.CreateObject("InternetExplorer.Application","ie_")
ie.Visible = true;
ie.Navigate("http://myurl.com");
for(;ie.ReadyState<4;);
ie.document.all.item("login").value = "username";
ie.document.all.item("password").value = "password";
ie.document.getElementById("Action").click();

Open in new window

0
Comment
Question by:ButtonDownBobby
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24104521
I would guess you need to wait for readystate 4 again before you click the link
0
 

Author Comment

by:ButtonDownBobby
ID: 24106615
I already do the rest of the test code is the following but i'm not sure how i can have a 'click' triggered for the anchor item rather than the button.  I mean the code is close enough but i'm all thumbs with this right now.  
for(;ie.ReadyState<4;);
 
// search field
HtmlElement search = ie.Document.Body.All["q"];
search.SetAttribute("value", "something to search");
// button to click
HtmlElement htmlButton = ie.Document.Body.All["btnG"];
// need the DOM version of the element
object domHtmlButton = htmlButton.DomElement;
// get the method information for the click event
System.Reflection.MethodInfo mi = domHtmlButton.GetType().GetMethod("click");
// invoke the method
mi.Invoke(domHtmlButton, new object[0]);

Open in new window

0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 150 total points
ID: 24106627
document.getElementsByTagName('a')
or document.links
would give you the arrays you need
0
RoboForm Secure Password Management System

RoboForm Everywhere - Superb Browser Support
Windows / Apple / IOS / Android / Linux / Chrome OS
Use different complex passwords everywhere
Best Secure Password Management by far
Synchronize all of your devices instantly
Safe, Secure & Highly Recommended!

 

Author Comment

by:ButtonDownBobby
ID: 24111303
I'm still working on this but is there any way to call the "onClick="onTabClick('reports',getForm(this)); return false;""  function by itself?

I would like to just trigger the onClick event run the onTabClick and pass the given paramets...
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24114662
depends on what getForm needs from the object clicked
0
 

Accepted Solution

by:
ButtonDownBobby earned 0 total points
ID: 24139386
I got it!!!  what i was looking for was the "fireEvent" method!!!
ie = WScript.CreateObject("InternetExplorer.Application","ie_")
ie.Visible = true;
ie.Navigate("http://myurl.com");
for(;ie.ReadyState<4;);
ie.document.all.item("login").value = "username";
ie.document.all.item("password").value = "password";
ie.document.getElementById("Action").click();
do{
	WScript.Sleep(200);
}
while (ie.busy);
 
links = ie.document.getElementById("tabs").getElementsByTagName("a");
 
for(var i = 0; i < links.length; i++) {
	if (links[i].innerText == "text in the anchor") {
		links[i].fireEvent("onClick");
		break;
	}
}

Open in new window

0
 

Author Comment

by:ButtonDownBobby
ID: 24139500
It was a good start but, in the end there was more legwork done on my end looking for the solution.  I'll give you credit for helping with looking for the  getElementsByTagName.  Hopefully we can work together in the future if I run across any more javascript issues. :)  
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24140624
So in WSH It is not links[i].ciick() ??
Bizarre :)
0
 

Author Comment

by:ButtonDownBobby
ID: 24157506
Yea, I was expecting links[i].click() to work but, fireEvent got it done...  You can check the code it runs fine and I'm running with this version until/unless I find something better.  Maybe the click method does work but i was using it wrong or had the wrong case...  ohh well the major thing is this worked out for me. :D

I'm now playing with DOMs and traversing children items now.  It's fun now that I can get past some of the rudimentary stuff.  I'm also looking into Microsoft's PowerShell as an alternate for WSH.  Having the ability to automate some of the more mundane routines is great!!!

Again thanks for the help.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Jquery form 9 64
ads on IE - How do I clear them? 5 35
SSL unsecure page mystery 17 46
How to build a logic for passwords according to initials? 13 46
This article offers some helpful and general tips for safe browsing and online shopping. It offers simple and manageable procedures that help to ensure the safety of one's personal information and the security of any devices.
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Google currently has a new report that is in beta and coming soon to Webmaster Tool accounts. This Micro Tutorial will highlight new features for Google Webmaster Tools.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question