如何以编程方式单击由javascript动态生成的链接 [英] How to programmatically click on link dynamically generated by javascript

查看:68
本文介绍了如何以编程方式单击由javascript动态生成的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序可以通过InternetExplorer读取特定的网页.
在此网页上动态(通过javascript)创建了链接<a id="link2" onclick="some javascript code" >Click</a>.
我需要找到此链接并模拟单击它,并启动一些javascript代码.
生成的代码不是页面源代码的一部分,并且我不知道是否可能.有什么建议吗?请帮忙.

我的代码是用vb.net编写的.请参阅示例代码.

I have a program that reads specific web page via InternetExplorer.
On this web page is dynamically (by javascript) created link <a id="link2" onclick="some javascript code" >Click</a>.
I need to find this link and to simulate click on it and to start some javascript code.
The generated code is not a part of page source code and I don''t know if it is possible. Have you any suggestions? Please help.

My code is written in vb.net. See sample code.

Dim ie as New InternetExplorer
Dim mVal As Object = System.Reflection.Missing.Value

ie.Navigate(url, mVal, mVal, mVal, mVal)

While ie.ReadyState <> tagREADYSTATE.READYSTATE_COMPLETE
   System.Threading.Thread.Sleep(500)
End While

Dim link1 As HTMLAnchorElement = ie.Document.all.item("link1")
link1.click() // now javascript generates couple of html code (also some link2)

Dim link2 As HTMLAnchorElement = ie.Document.all.item("link2") // it does not work, because generated html is not in Document - link2 is Nothing
link2.click()

推荐答案

在Web应用程序中没有程序化单击"之类的东西.您可以使用Windows API SendInput模拟键盘或鼠标,但这很脏,我不想讨论.如果您考虑一下,您将理解这不是您真正需要的.您只需要具有与点击相同的效果即可.

首先,您在HTML中显示的锚点在点击时不起作用:
There is no such thing as "programmatic click", not in Web applications. You can simulate a keyboard or mouse with Windows API SendInput, but this is dirty, I don''t want to discuss it. If you think about it, you will understand that this is not what you really need. You only need to have the same effect as the click, that''s it.

First of all, your anchor you show in your HTML does not do anything on click:
<a id="link1">Click</a> <!-- nothing happens on click -->


假设您说对了:


Let''s assume you put it right:

<a href="someUrl.html" id="link1">Click</a> <!-- a click will redirect to someUrl.html -->



然后,您可以通过id或其他方式找到此元素,只需提取URL并使用ie.Navigate将页面重定向到该元素即可.

—SA



Then you can find this element by id or otherwise and simply extract the URL and re-direct the page to it using ie.Navigate.

—SA


这篇关于如何以编程方式单击由javascript动态生成的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆