vb.net 如何在visual basic中单击网页上的链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15283960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to click links on a webpage in visual basic
提问by sociallymellow
I have searched for how to click links on a webpage in vb before, and got a good answer on this site actually. But now I am trying to click another link, and let me just post it's code so it's easier to understand. (First time ever asking a question, so go easy on me lol)
我之前搜索过如何在 vb 中点击网页上的链接,实际上在这个网站上得到了很好的答案。但现在我试图点击另一个链接,让我发布它的代码,以便更容易理解。(第一次提问,所以放轻松,哈哈)
<div class="pauseButton" style="display: block;"><a href="#" address="true"></a></div>
Here is my code (This is for Pandora btw, and here is my code for it to sign you in.)
这是我的代码(顺便说一句,这是针对 Pandora 的,这是我用来登录的代码。)
Public Class fMain
Dim elementCollection As HtmlElementCollection
Private Sub fMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
wb.Navigate("http://www.pandora.com")
End Sub
'buttons
Private Sub bLogin_Click(sender As Object, e As EventArgs) Handles bLogin.Click
elementCollection = wb.Document.GetElementsByTagName("input")
For Each ele As HtmlElement In elementCollection
If ele.GetAttribute("name").Equals("email") Then
ele.SetAttribute("Value", tbEmail.Text)
End If
If ele.GetAttribute("name").Equals("password") Then
ele.SetAttribute("Value", tbPassword.Text)
End If
If ele.GetAttribute("type") = "submit" Then
ele.InvokeMember("click")
End If
Next
End Sub
Private Sub bSignOut_Click(sender As Object, e As EventArgs) Handles bSignOut.Click
End Sub
Private Sub bPlay_Click(sender As Object, e As EventArgs) Handles bPlay.Click
Dim IsRightElement As Boolean = False
For Each ele As HtmlElement In wb.Document.Links
If IsRightElement Then
ele.InvokeMember("click")
IsRightElement = False
Exit For
End If
If ele.GetAttribute("class") = "playButton" Then
IsRightElement = True
End If
Next
End Sub
Private Sub bPause_Click(sender As Object, e As EventArgs) Handles bPause.Click
End Sub
Private Sub bFavorite_Click(sender As Object, e As EventArgs) Handles bFavorite.Click
End Sub
End Class
Any help would be greatly appreciated, and sorry if I made the question confusing, but pretty much I know how to click a link with specific href="link.com" but here, the only thing distinguishing the play button with any other button is its the href="#" so that is not much help. Again thanks in advanced. (:
任何帮助将不胜感激,如果我让问题令人困惑,我很抱歉,但我几乎知道如何单击具有特定 href="link.com" 的链接,但在这里,唯一将播放按钮与任何其他按钮区分开来的是它的 href="#" 所以没有多大帮助。再次感谢先进。(:
EDIT: I am trying to make a Pandora streamer, I should have mentioned that earlier.
编辑:我正在尝试制作 Pandora 流光,我应该早点提到。
采纳答案by Jeremy Thompson
In the HTML I give the link an ID:
在 HTML 中,我给链接一个 ID:
<div class="pauseButton" style="display: block;"><a id="LinkID" href="#" address="true"></a></div>
VB.Net code to get the element by Id and invoke it:
通过 Id 获取元素并调用它的 VB.Net 代码:
Private Function ClickSubmitButton()
Dim theButton As HtmlElement
Try
' Link the ID from the web form to the Button var
theButton = webbrowser1.Document.GetElementById("LinkID")
' Now do the actual click.
theButton.InvokeMember("click")
Return True
Catch ex As Exception
Return False
End Try
End Function
Update:
更新:
Ok without the LinkID you need to loop through all elements, once you identify the DIV, you know the next element is the link...
好的,如果没有 LinkID,您需要遍历所有元素,一旦识别出 DIV,就知道下一个元素是链接...
Dim IsRightElement as Boolean = False
For Each ele As HtmlElement In wb.Document.Links
If IsRightElement Then
ele.InvokeMember("click")
IsRightElement = False
Exit For
End If
If ele.GetAttribute("class") = "playButton" Then
IsRightElement = True
End If
Next
回答by kirbyfan64sos
If you're using Visual Studio.NET, do this: 1.Drop a HyperLink Control 2.Under the NavigateUrl property, click the ellipsis. You should now get a screen with all the files in your project 3.Select your HTML file. If it isn't part of your project, add it using Browse.
如果您使用的是 Visual Studio.NET,请执行以下操作: 1. 拖放超链接控件 2. 在 NavigateUrl 属性下,单击省略号。您现在应该会看到一个包含项目中所有文件的屏幕 3. 选择您的 HTML 文件。如果它不是您项目的一部分,请使用浏览添加它。

