0

I want to simulate a click on a element on a website. The website contains a list and every element is expandable. The element i want to click on causes an entry of the list to expand, so its something like a "show detailed info" button.

First, here is the html code where the button is defined:

<td>
   <span id="span_details62_0">
     <a href="#" onclick="setBusy(&#x27;running-indication&#x27;);
            render_event_cluster(&#x27;104,105&#x27;, 620);
            toggle_fe(&#x27;dtlsRow620&#x27;);
            change(&#x27;span_details62_0&#x27;, &#x27;show&#x27;);
            Element.update(&#x27;running-indication&#x27;,&#x27;&#x27;);; return false;"><img alt="show | hide" border="0" height="10" src="/images/layout/general/clear.gif?1408570117" title="Show/hide details" width="10" /></a>
   </span>
</td>

Is it possible to send the commands

        render_event_cluster(&#x27;104,105&#x27;, 620);
        toggle_fe(&#x27;dtlsRow620&#x27;);
        change(&#x27;span_details62_0&#x27;, &#x27;show&#x27;);

directly to the website, thus making it think that the button has been click and the result are showed?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Helyx
  • 329
  • 1
  • 5
  • 17

2 Answers2

0

Yes, of course you can with page.evaluate(). You will need to "decode" (&#x27; is an HTML entity for ') the JavaScript first:

page.evaluate(function(){
    setBusy('running-indication');
    render_event_cluster('104,105', 620);
    toggle_fe('dtlsRow620');
    change('span_details62_0', 'show');
    Element.update('running-indication','');
});
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • I tried that and in the screenshot afterwards the icon of the button looks like it has been clicked but the list is NOT expanded, so i can't see more than before. Also the "620" from render_event_cluster('104,105', 620); changes everytime, does that matter? – Helyx Sep 01 '15 at 10:04
  • It probably does matter. You should debug it yourself. Also, you can see if there are errors when you do this: please register to the `onConsoleMessage`, `onError`, `onResourceError`, `onResourceTimeout` events ([Example](https://gist.github.com/artjomb/4cf43d16ce50d8674fdf)). – Artjom B. Sep 01 '15 at 10:06
  • How do I register the things you mentioned? Sorry I'm not very familiar with JavaScript / phantomjs – Helyx Sep 01 '15 at 10:14
  • Ah sorry, didn't see the link. I included you code in my script but there is no additional output. Is that normal? – Helyx Sep 01 '15 at 10:22
  • It means that there is no error, so you're on your own. – Artjom B. Sep 01 '15 at 10:24
  • Ah wait a sec. I think i made a mistake. First, here is my entire script: http://pastebin.com/dKwu2Zg5 and here is the new console output with your code: http://pastebin.com/TA31q901 – Helyx Sep 01 '15 at 10:32
  • Oh and i noticed that the "620" does not change everytime. It only changed because there is a new entry in the list. – Helyx Sep 01 '15 at 10:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88465/discussion-between-helyx-and-artjom-b). – Helyx Sep 01 '15 at 10:45
0

the click method should work for you:

var a = document.getElementById('span_details62_0').getElementsByTagName('a')[0];
a.click();
Lukas Kral
  • 987
  • 13
  • 22
  • That does not work. a.click is not defined. If i try this: var a = document.getElementById("span_details62_2"); var e = document.createEvent('MouseEvents'); e.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); a.dispatchEvent(e); a.waitforload = true; e.waitforload = true; it executes the code, but nothing happens – Helyx Sep 01 '15 at 10:08