user797257
user797257

Reputation:

Automatically click a link and navigate to another page

I need to test a page tracker behavior. The tracker's reporting triggers when links are clicked, however generating a click event on anchor elements doesn't instruct the browser to navigate to the page in the link. I tried Selenium IDE - to no effect.

Is there any way to accomplish this? Perhaps some browser setting? If it is specific to Firefox or Google Chrome or Opera - it doesn't matter, I could use any of those for testing.

Here's a similar question How do I programmatically click a link with javascript? but it doesn't help in my case, as as I just described, no navigation happens in this case.

Upvotes: 0

Views: 2919

Answers (2)

Filippos Karapetis
Filippos Karapetis

Reputation: 4652

Normally, clicking on an anchor link takes you somewhere else inside the page and changes the anchor link to foo.php#bar (where bar is the name of the anchor), so this behavior is by design.

Here are all the different methods I can think of

Method 1: adapt your tracker

Perhaps you can configure your tracker to track these changes? Clicking on an anchor fires up the hashchange event. Check this SO question

Check these links for ways to utilize the hashchange event:

Method 2: use the HTML5 history API

You can change your browser's URLs programmatically via the HTML5 pushState() method, i.e. the history API.

Here are some examples of utilizing the HTML history API:

Upvotes: 1

Yuriy Galanter
Yuriy Galanter

Reputation: 39777

 <a id="gLink" href="http://www.google.com">Google</a>

<script>
    document.getElementById("gLink").click()
</script>

Does the trick.

Upvotes: 1

Related Questions