[英]Vb.net click link by looking for String!
I am working on a vb.net program, I want to click a hyperlink on a page, the source look like this: 我正在使用vb.net程序,我想单击页面上的超链接,源看起来像这样:
<a href="user_messages_view.php?id=23112">messages for Today, 2010-10-19 </a>
I want to check it everyday too! 我也想每天检查一次!
I tried to click it with the following methods(Both couldn't click the link!): 我尝试使用以下方法单击它(两个都无法单击链接!):
Dim theElementCollection As HtmlElementCollection
Dim ctrlIdentity As String
theElementCollection = WebBrowser1.Document.GetElementsByTagName("a")
For Each curElement As HtmlElement In theElementCollection
ctrlIdentity = curElement.GetAttribute("innerText").ToString
If ctrlIdentity = Today.Date.ToString(Today.Date.ToString("dd")) Then
curElement.InvokeMember("click")
End If
Next
and I tried this code too: 我也尝试了这段代码:
If Me.WebBrowser1.Document.Links(i).InnerHtml.Contains(Today.Date.ToString("dd")) Then
Me.WebBrowser1.Document.Links(i).InvokeMember("Click")
End If
Next
Any help would be appreciated! 任何帮助,将不胜感激! Thanks!
谢谢!
I've found that the best way to click links in a WebBrowser is using javascript. 我发现在WebBrowser中单击链接的最佳方法是使用javascript。 Try something like this:
尝试这样的事情:
WebBrowser1.Navigate("javascript:function%20x(){document.getElementById('foo').click()}x()")
You'll need to rewrite your above code in javascript but that's a piece of cake. 您需要使用javascript重写上述代码,但这只是小菜一碟。 You can test your javascript by copy-pasting it directly into the browser's location bar.
您可以通过将JavaScript直接复制粘贴到浏览器的位置栏中来测试您的JavaScript。 This is also a reliable way to fill out forms.
这也是填写表格的可靠方式。
Caveats: 注意事项:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.