简体   繁体   English

Vb.net通过查找字符串单击链接!

[英]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: 注意事项:

  • Notice how the work that I want to do is wrapped in a function. 注意,我想做的工作是如何包装在一个函数中的。 This is needed if you want the javascript to do multiple statements. 如果您希望javascript执行多个语句,则需要这样做。 Wrap in a function and then invoke the function. 包装一个函数,然后调用该函数。
  • You can't navigate to a URL more than around 500 characters. 您所浏览的网址不能超过500个字符。 (The limit isn't exactly 512 but it's close.) There's no warning, either, so keep it in mind. (限制并非完全是512,但接近。)也没有警告,请记住这一点。
  • Make sure you wait until the page is loaded. 确保等到页面加载完毕。 The ReadyState = Complete and IsBusy = False. ReadyState =完成,IsBusy = False。
  • Clicking like this doesn't always generate the usual events that you get when you click a link. 像这样单击并不总是会生成单击链接时获得的常规事件。
  • "%20" is hex for space. “%20”是空格的十六进制。 I don't recall if this was strictly necessary in my code. 我不记得这在我的代码中是否绝对必要。 Try it both ways. 尝试两种方式。

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

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