1

I am trying to be able to automate the process of clicking on links built in javascript using watin.

In the source code of web page, the links look like href="#", so i think these links works with Javascript.

Runscript doesn't work with a Javascript code like "link.click". When i use a "link.flash()" i can see the link flash so i'm sure the script click on the links but there is not effect ( and the script doesn't crash ) .

Have you any ideas for resolve this problem ?

edit : The links class are generate automatically

here the HTML code : All the * are name generate automatically

<div class="******" id="ext-gen***">
    <ul class="**********" id="ext-gen***">
        <li class="******" id="***************">
           <a class"*****" href="#" id="ext-gen***">
              <em class="********">
                 <span class="*******">
                    <span class="*********"> LINK 1 </span>
                 </span>
              </em>
           </a>
        </li>
        <li class="******" id="***************">
           <a class"*****" href="#" id="ext-gen***">
              <em class="********">
                 <span class="*******">
                    <span class="*********"> LINK 2 </span>
                 </span>
              </em>
           </a>
        </li>

etc.....

Yeah it's horrible ... , the only solution is to select by the Text into the div.

It's working in the slate javascript in the tools of Firefox but when I insert it Runscript with watin, it doesn't work.

here it's the code with Runscript :

StringBuilder myScript = new StringBuilder();

myScript.AppendLine("javascript: ");
myScript.AppendLine(" var link = document.getElementsByTagName('a'), i;");
myScript.AppendLine(" for (i in link)");
myScript.AppendLine(" {");
myScript.AppendLine("    if ((' ' + link[i].text + ' ').indexOf(' ' + '" + name + "' + ' ') > -1) ");
myScript.AppendLine("    {");
myScript.AppendLine("       link[i].click();");
myScript.AppendLine("        break; ");
myScript.AppendLine("    }");
myScript.AppendLine(" }");
Thread.Sleep(100);
ie.RunScript(myScript.ToString());
BEC Roland
  • 25
  • 6

1 Answers1

1

Just fire the javascript code if the link class is "linkClass" run this command:

browser.Eval("$('.linkClass').click();")

Edit: Do you need to perform some action between each link click?

Do not use RunScript command i had bad experience with it.

You can try this:

   var links = ie.Links;
   foreach (var link in links)
   {
       ie.Eval(string.Format("$('#{0}').click();", link.Id));
       //after click do your test (or not)...
   }
alonp
  • 1,307
  • 2
  • 10
  • 22