简体   繁体   English

自动单击链接并导航到另一个页面

[英]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. 我试过Selenium IDE - 没效果。

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. 如果它特定于Firefox或谷歌Chrome或Opera - 没关系,我可以使用其中任何一个进行测试。

Here's a similar question How do I programmatically click a link with javascript? 这是一个类似的问题如何以编程方式单击带有javascript的链接? but it doesn't help in my case, as as I just described, no navigation happens in this case. 但在我的情况下它并没有帮助,正如我刚才所描述的那样,在这种情况下不会发生导航。

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

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

Does the trick. 诀窍。

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. 通常,单击锚链接会将您带到页面内的其他位置,并将锚链接更改为foo.php #bar(其中bar是锚的名称),因此此行为是设计使然。

Here are all the different methods I can think of 以下是我能想到的所有不同方法

Method 1: adapt your tracker 方法1:调整您的跟踪器

Perhaps you can configure your tracker to track these changes? 也许您可以配置跟踪器来跟踪这些更改? Clicking on an anchor fires up the hashchange event. 单击一个锚点会激活hashchange事件。 Check this SO question 检查这个问题

Check these links for ways to utilize the hashchange event: 检查这些链接以了解如何使用hashchange事件:

Method 2: use the HTML5 history API 方法2:使用HTML5历史API

You can change your browser's URLs programmatically via the HTML5 pushState() method, ie the history API. 您可以通过HTML5 pushState()方法(即历史记录API)以编程方式更改浏览器的URL。

Here are some examples of utilizing the HTML history API: 以下是使用HTML历史记录API的一些示例:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM