-
Oct 22nd, 2014, 03:15 PM
#1
Thread Starter
Lively Member
[RESOLVED] Clicking Element
I am trying to click on a button on a webpage that matches what I have selected in my ListView. I am grabbing a list of items from a webpage, and organizing them into a ListView. Here is my code for that:
Code:
Dim HTML As String = New WebClient().DownloadString("MY URL IS HERE")
HTML = Regex.Match(HTML, "<ul class=""songullist-orange"">(.+?)</ul>", RegexOptions.Singleline).Groups(1).ToString
Dim Musics_Regex As MatchCollection = Regex.Matches(HTML, "<li.+?>(.+?)</li>")
Dim Musics_List(Musics_Regex.Count - 1) As String
Dim Index As Integer
ListView1.Items.Clear()
For Each Match As Match In Musics_Regex
Musics_List(Index) = RemoveTags(Match.Groups(1).ToString)
Dim tagsRemoved As String = Musics_List(Index)
Dim songName As String = tagsRemoved.Substring(0, tagsRemoved.IndexOf(" -"))
Dim artist As String = tagsRemoved.Substring(tagsRemoved.IndexOf("- ") + 2)
st(0) = songName
st(1) = artist
item = New ListViewItem(st)
ListView1.Items.Add(item)
Index += 1
Next
All of the above code works perfectly. Now my question: Let's say I clicked on one of the rows in my ListView to select it. Next, I want to click a button and by clicking that button, it is finding that corresponding text on the webpage and clicking on that one. I hope I made myself somewhat clear. I am a little bit of a rookie at this kind of stuff so here is what I tried but failed. I'm pretty sure I am waaaaaay off.
Code:
For Each Element As HtmlElement In Musics_Regex
If ListView1.SelectedItems.Item(0).SubItems(0).Text = Musics_List(Index) Then
Element.InvokeMember("click")
End If
Next
-
Oct 22nd, 2014, 04:15 PM
#2
Re: Clicking Element
When you say it failed, what exactly is occurring? Is an exception being thrown or is it just not doing anything - or does it do something other than what you want?
I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
My war with a browser-redirect trojan
-
Oct 22nd, 2014, 04:17 PM
#3
Thread Starter
Lively Member
Re: Clicking Element
Originally Posted by dolot
When you say it failed, what exactly is occurring? Is an exception being thrown or is it just not doing anything - or does it do something other than what you want?
In the line "For Each Element As HtmlElement In Musics_Regex" there is an error. The error states "Unable to cast object of type 'System.Text.RegularExpressions.Match' to type 'System.Windows.Forms.HtmlElement'" pointing to "Musics_Regex."
-
Oct 22nd, 2014, 04:33 PM
#4
Re: Clicking Element
Musics_Regex is an array of regex matches, and not of HtmlElements, this is why your code isn't working. I belive this code was meant to be used with the webbrowser control.
So, you said you want to simulate a "click" on the link. This would load a new page. What exactly you want to do? Grab more data from the page that will open, or just display the page to the user?
The following code will show the URL address of the details page when user click on the listview item (replace with the actual URLs on needed places):
Code:
Private Structure Search_Result
Dim Music_Name As String
Dim Page_URL As String
End Structure
Dim Musics_List() As Search_Result
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim st() As String
Dim HTML As String = New WebClient().DownloadString("PUT YOUR URL HERE")
HTML = Regex.Match(HTML, "<ul class=""songullist-orange"">(.+?)</ul>", RegexOptions.Singleline).Groups(1).ToString
Dim Musics_Regex As MatchCollection = Regex.Matches(HTML, "<li.+?><a href=""(.+?)"" target=""_self"">(.+?)</li>")
ReDim Musics_List(Musics_Regex.Count - 1)
Dim Index As Integer
ListView1.Items.Clear()
For Each Match As Match In Musics_Regex
Musics_List(Index).Page_URL = "http://PUT THE ROOT ADDRESS YOUR URL HERE" & Match.Groups(1).ToString
Musics_List(Index).Music_Name = RemoveTags(Match.Groups(2).ToString)
st = Musics_List(Index).Music_Name.Split("-")
ListView1.Items.Add(New ListViewItem("Artist: " & st(0).Trim & " / Music: " & st(1).Trim))
Index += 1
Next
End Sub
Private Function RemoveTags(ByVal Str As String) As String
Dim New_Str As String = Regex.Replace(Str, "<[/]?(.+?)>", "").Trim
New_Str = Replace(New_Str, " ", " ")
New_Str = Replace(New_Str, "&", "&")
Return New_Str
End Function
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
If ListView1.SelectedItems.Count > 0 Then
Dim First_Selected_Item As Integer = ListView1.SelectedItems(0).Index
MsgBox(Musics_List(First_Selected_Item).Page_URL)
End If
End Sub
-
Oct 22nd, 2014, 04:51 PM
#5
Thread Starter
Lively Member
Re: Clicking Element
I figured this would be difficult to explain. Lol. The items that are displayed on the webpage (the WebBrowser is hidden) are in list form and can be clicked on. As you know from helping me out on my other thread, the list is taken from the webpage and displayed in a ListView. Now, because this WebBrowser is hidden, i want to highlight one of the items in the ListView and then click on a button on my form to visit that song's respective webpage. I hope I somewhat cleared things up.
-
Oct 22nd, 2014, 04:56 PM
#6
Re: Clicking Element
Originally Posted by nyyankeesfan28
I figured this would be difficult to explain. Lol. The items that are displayed on the webpage (the WebBrowser is hidden) are in list form and can be clicked on. As you know from helping me out on my other thread, the list is taken from the webpage and displayed in a ListView. Now, because this WebBrowser is hidden, i want to highlight one of the items in the ListView and then click on a button on my form to visit that song's respective webpage. I hope I somewhat cleared things up.
Understood. Add the button and use:
Code:
If ListView1.SelectedItems.Count > 0 Then
Dim First_Selected_Item As Integer = ListView1.SelectedItems(0).Index
WebBrowser1.Visible = True
WebBrowser1.Navigate(Musics_List(First_Selected_Item).Page_URL)
End If
This will open the music page on the webbrowser control.
(also, say if you don't understand something on the code and I will explain, because I think you will not learn stuff if I keep posting just the code XD)
Last edited by omundodogabriel; Oct 22nd, 2014 at 05:03 PM.
-
Oct 22nd, 2014, 05:07 PM
#7
Thread Starter
Lively Member
Re: Clicking Element
I must not be understanding something. How would I find my root address? Sorry for all this frustration I must be causing you. KryptonButton3 is clicked first to show the list of songs in my ListView and then KryptonButton2 is clicked to visit the webpage of the song. And once we figure out the correct code, can you please explain it? As you said, I am not learning anything. Couldn't thank you more! Here is my code:
Code:
Private Structure Search_Result
Dim Music_Name As String
Dim Page_URL As String
End Structure
Private Sub KryptonButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton2.Click
Dim Musics_List() As Search_Result
Dim st() As String
Dim HTML As String = New WebClient().DownloadString("PUT YOUR URL HERE")
HTML = Regex.Match(HTML, "<ul class=""songullist-orange"">(.+?)</ul>", RegexOptions.Singleline).Groups(1).ToString
Dim Musics_Regex As MatchCollection = Regex.Matches(HTML, "<li.+?><a href=""(.+?)"" target=""_self"">(.+?)</li>")
ReDim Musics_List(Musics_Regex.Count - 1)
Dim Index As Integer
ListView1.Items.Clear()
For Each Match As Match In Musics_Regex
Musics_List(Index).Page_URL = "http://PUT THE ROOT ADDRESS YOUR URL HERE" & Match.Groups(1).ToString
Musics_List(Index).Music_Name = RemoveTags(Match.Groups(2).ToString)
Next
If ListView1.SelectedItems.Count > 0 Then
Dim First_Selected_Item As Integer = ListView1.SelectedItems(0).Index
MainForm.WebBrowser1.Visible = True
MainForm.WebBrowser1.Navigate(Musics_List(First_Selected_Item).Page_URL)
End If
id3.Read(Me.AxWindowsMediaPlayer1.URL)
id3.Title = ListView1.SelectedItems.Item(0).SubItems(0).Text
id3.Artist = ListView1.SelectedItems.Item(0).SubItems(1).Text
'id3.Album=
If FileInUse(Me.AxWindowsMediaPlayer1.URL) = True Then
MsgBox("The file you opened is currently in use! Please close any programs that are using the file", MsgBoxStyle.Exclamation, "File In Use!")
Else
id3.Write()
End If
End Sub
Private Sub KryptonButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton3.Click
If KryptonTextBox1.Text = "" Or KryptonTextBox1.Text = "Please enter the name of the song here" Then
MsgBox("Please enter the name of the song in the field provided.", MsgBoxStyle.Exclamation, "Song Name")
namenotempty = False
Else
MainForm.WebBrowser1.Navigate("MY URL")
namenotempty = True
WaitUntilPageLoads()
Try
Dim PageElement As HtmlElementCollection = MainForm.WebBrowser1.Document.GetElementsByTagName("h2")
For Each CurElement As HtmlElement In PageElement
If CurElement.OuterText.Contains("Sorry, we don't have any results for the Song name") Then
MsgBox("We couldn't find any songs named '" + KryptonTextBox1.Text + ".' Please try again.", MsgBoxStyle.Exclamation, "No Songs Found!")
HideButtons()
ElseIf CurElement.OuterText.Contains("Search results for") Then
Dim HTML As String = New WebClient().DownloadString("MY URL")
HTML = Regex.Match(HTML, "<ul class=""songullist-orange"">(.+?)</ul>", RegexOptions.Singleline).Groups(1).ToString
Dim Musics_Regex As MatchCollection = Regex.Matches(HTML, "<li.+?>(.+?)</li>")
Dim Musics_List(Musics_Regex.Count - 1) As String
Dim Index As Integer
ListView1.Items.Clear()
For Each Match As Match In Musics_Regex
Musics_List(Index) = RemoveTags(Match.Groups(1).ToString)
Dim tagsRemoved As String = Musics_List(Index)
Dim songName As String = tagsRemoved.Substring(0, tagsRemoved.IndexOf(" -"))
Dim artist As String = tagsRemoved.Substring(tagsRemoved.IndexOf("- ") + 2)
st(0) = songName
st(1) = artist
item = New ListViewItem(st)
ListView1.Items.Add(item)
Index += 1
Next
ShowButtons()
End If
Next
Catch ex As Exception
End Try
End If
-
Oct 22nd, 2014, 05:15 PM
#8
Re: Clicking Element
What I called root page is the address of the page before the "/"
Example, on the page "https://www.google.com.br/?gws_rd=ssl#q=hanasaku+iroha" the root page is "https://www.google.com.br"
Edit:
Looks like you messed the code a bit. The code I provided don't need the webbrowser to download the page.
To make things easier, this code goes on KryptonButton3:
Code:
Dim st() As String
Dim HTML As String = New WebClient().DownloadString("PUT YOUR URL HERE")
HTML = Regex.Match(HTML, "<ul class=""songullist-orange"">(.+?)</ul>", RegexOptions.Singleline).Groups(1).ToString
Dim Musics_Regex As MatchCollection = Regex.Matches(HTML, "<li.+?><a href=""(.+?)"" target=""_self"">(.+?)</li>")
ReDim Musics_List(Musics_Regex.Count - 1)
Dim Index As Integer
ListView1.Items.Clear()
For Each Match As Match In Musics_Regex
Musics_List(Index).Page_URL = "http://PUT THE ROOT ADDRESS YOUR URL HERE" & Match.Groups(1).ToString
Musics_List(Index).Music_Name = RemoveTags(Match.Groups(2).ToString)
st = Musics_List(Index).Music_Name.Split("-")
ListView1.Items.Add(New ListViewItem("Artist: " & st(0).Trim & " / Music: " & st(1).Trim))
Index += 1
Next
And this one on KryptonButton2:
Code:
If ListView1.SelectedItems.Count > 0 Then
Dim First_Selected_Item As Integer = ListView1.SelectedItems(0).Index
WebBrowser1.Visible = True
WebBrowser1.Navigate(Musics_List(First_Selected_Item).Page_URL)
End If
And this on top of the form:
Code:
Private Structure Search_Result
Dim Music_Name As String
Dim Page_URL As String
End Structure
Dim Musics_List() As Search_Result
Once you get this basic part working, then you start adding the other part you want, and I will explain parts you don't uderstand.
Last edited by omundodogabriel; Oct 22nd, 2014 at 05:22 PM.
-
Oct 22nd, 2014, 05:24 PM
#9
Thread Starter
Lively Member
Re: Clicking Element
Well it didn't work. Just to make sure you understand what I want, I want the WebBrowser to navigate to the song's specific webpage.
Edit:
Hold on. Let me try what you said in your edit.
-
Oct 22nd, 2014, 08:20 PM
#10
Thread Starter
Lively Member
Re: Clicking Element
Something still isn't working...
-
Oct 22nd, 2014, 08:21 PM
#11
Re: Clicking Element
Originally Posted by nyyankeesfan28
Something still isn't working...
Weird, it should work... Can you post the entire code?
-
Oct 22nd, 2014, 08:59 PM
#12
Thread Starter
Lively Member
Re: Clicking Element
Originally Posted by omundodogabriel
Weird, it should work... Can you post the entire code?
Code:
Private Structure Search_Result
Dim Music_Name As String
Dim Page_URL As String
End Structure
Dim Musics_List() As Search_Result
Private Sub KryptonButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton2.Click
If ListView1.SelectedItems.Count > 0 Then
Dim First_Selected_Item As Integer = ListView1.SelectedItems(0).Index
MainForm.WebBrowser1.Navigate(Musics_List(First_Selected_Item).Page_URL)
End If
End Sub
Private Function RemoveTags(ByVal Str As String) As String
Dim New_Str As String = Regex.Replace(Str, "<[/]?(.+?)>", "").Trim
New_Str = Replace(New_Str, "</a>", "")
New_Str = Replace(New_Str, " ", " ")
New_Str = Replace(New_Str, "&", "&")
New_Str = Replace(New_Str, "'", "'")
Return New_Str
End Function
Private Sub KryptonButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton3.Click
If KryptonTextBox1.Text = "" Or KryptonTextBox1.Text = "Please enter the name of the song here" Then
MsgBox("Please enter the name of the song in the field provided.", MsgBoxStyle.Exclamation, "Song Name")
namenotempty = False
Else
MainForm.WebBrowser1.Navigate("MY URL")
namenotempty = True
WaitUntilPageLoads()
Try
Dim PageElement As HtmlElementCollection = MainForm.WebBrowser1.Document.GetElementsByTagName("h2")
For Each CurElement As HtmlElement In PageElement
If CurElement.OuterText.Contains("Sorry, we don't have any results for the Song name") Then
MsgBox("We couldn't find any songs named '" + KryptonTextBox1.Text + ".' Please try again.", MsgBoxStyle.Exclamation, "No Songs Found!")
HideButtons()
ElseIf CurElement.OuterText.Contains("Search results for") Then
Dim st() As String
Dim HTML As String = New WebClient().DownloadString("PUT YOUR URL HERE")
HTML = Regex.Match(HTML, "<ul class=""songullist-orange"">(.+?)</ul>", RegexOptions.Singleline).Groups(1).ToString
Dim Musics_Regex As MatchCollection = Regex.Matches(HTML, "<li.+?><a href=""(.+?)"" target=""_self"">(.+?)</li>")
ReDim Musics_List(Musics_Regex.Count - 1)
Dim Index As Integer
ListView1.Items.Clear()
For Each Match As Match In Musics_Regex
Musics_List(Index).Page_URL = "http://PUT THE ROOT ADDRESS YOUR URL HERE" & Match.Groups(1).ToString
Musics_List(Index).Music_Name = RemoveTags(Match.Groups(2).ToString)
st = Musics_List(Index).Music_Name.Split("-")
ListView1.Items.Add(New ListViewItem("Artist: " & st(0).Trim & " / Music: " & st(1).Trim))
Index += 1
Next
ShowButtons()
End If
Next
Catch ex As Exception
End Try
End If
End Sub
Obviously I replaced the URL's with the real ones. I'm not even getting the items to show up in my ListView anymore with this new code. Is it worth mentioning that I have my ListView broken into 2 columns?
Edit:
And there is more code involved in this form, but none of which is relevant to this problem. I figured I would let you know that. Lol
-
Oct 22nd, 2014, 09:11 PM
#13
Re: Clicking Element
try commenting this part:
Code:
Else
MainForm.WebBrowser1.Navigate("MY URL")
namenotempty = True
WaitUntilPageLoads()
Try
Dim PageElement As HtmlElementCollection = MainForm.WebBrowser1.Document.GetElementsByTagName("h2")
For Each CurElement As HtmlElement In PageElement
If CurElement.OuterText.Contains("Sorry, we don't have any results for the Song name") Then
MsgBox("We couldn't find any songs named '" + KryptonTextBox1.Text + ".' Please try again.", MsgBoxStyle.Exclamation, "No Songs Found!")
HideButtons()
Also, using a empty try/catch is not a good idea, since it will not show error that may be happening to you. I tested this code (the part I provided) on the HTML you provided in the other thread and it worked fine.
-
Oct 22nd, 2014, 09:19 PM
#14
Thread Starter
Lively Member
Re: Clicking Element
Nope. I don't get what I am doing wrong...
-
Oct 22nd, 2014, 09:26 PM
#15
Re: Clicking Element
try this:
Code:
Dim Musics_Regex As MatchCollection = Regex.Matches(HTML, "<li.+?>(.+?)</li>")
ReDim Musics_List(Musics_Regex.Count - 1)
Dim Index As Integer
ListView1.Items.Clear()
For Each Match As Match In Musics_Regex
Dim Data As String = Match.Groups(1).ToString
Musics_List(Index).Page_URL = "http://PUT THE ROOT ADDRESS YOUR URL HERE" & Regex.Match(Data, "href=""(.+?)""").Groups(1).ToString
Musics_List(Index).Music_Name = RemoveTags(Data)
st = Musics_List(Index).Music_Name.Split("-")
ListView1.Items.Add(New ListViewItem("Artist: " & st(0).Trim & " / Music: " & st(1).Trim))
Index += 1
Next
I will sleep now, if this doesn't work either, you can post the page url or the html source here if you want and I will take a look tomorrow.
-
Oct 22nd, 2014, 09:30 PM
#16
Thread Starter
Lively Member
Re: Clicking Element
Thank you so much for your help!! I will give that a try tomorrow as well as I need some sleep too. Sorry for the frustration..
-
Oct 23rd, 2014, 01:44 PM
#17
Thread Starter
Lively Member
Re: Clicking Element
Well I tried it. I created a new application just for testing the code to make sure that some part of the other code in my real application was screwing up and throwing something off and something is just a little bit off. I am getting correct results in the ListView, so I click on one of the songs, and click the button to visit that song's webpage and it is bringing me to some random song's webpage. Here is the ENTIRE code:
Code:
Imports System.Net
Imports System.Text.RegularExpressions
Public Class Form1
Private Structure Search_Result
Dim Music_Name As String
Dim Page_URL As String
End Structure
Dim Musics_List() As Search_Result
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim st() As String
Dim HTML As String = New WebClient().DownloadString("MY URL")
HTML = Regex.Match(HTML, "<ul class=""songullist-orange"">(.+?)</ul>", RegexOptions.Singleline).Groups(1).ToString
Dim Musics_Regex As MatchCollection = Regex.Matches(HTML, "<li.+?>(.+?)</li>")
ReDim Musics_List(Musics_Regex.Count - 1)
Dim Index As Integer
ListView1.Items.Clear()
For Each Match As Match In Musics_Regex
Dim Data As String = Match.Groups(1).ToString
Musics_List(Index).Page_URL = ("MY URL") & Regex.Match(Data, "href=""(.+?)""").Groups(1).ToString
Musics_List(Index).Music_Name = RemoveTags(Data)
st = Musics_List(Index).Music_Name.Split("-")
ListView1.Items.Add(New ListViewItem("Song: " & st(0).Trim & " / Artist: " & st(1).Trim))
Index += 1
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If ListView1.SelectedItems.Count > 0 Then
Dim First_Selected_Item As Integer = ListView1.SelectedItems(0).Index
WebBrowser1.Visible = True
WebBrowser1.Navigate(Musics_List(First_Selected_Item).Page_URL)
End If
End Sub
Private Function RemoveTags(ByVal Str As String) As String
Dim New_Str As String = Regex.Replace(Str, "<[/]?(.+?)>", "").Trim
New_Str = Replace(New_Str, "</a>", "")
New_Str = Replace(New_Str, " ", " ")
New_Str = Replace(New_Str, "&", "&")
New_Str = Replace(New_Str, "'", "'")
Return New_Str
End Function
End Class
-
Oct 23rd, 2014, 01:49 PM
#18
Re: Clicking Element
Well, this is a weird behaviour, but to find out the problem, show the browsed URL.
Below the "WebBrowser1.Visible = True", add the line "MsgBox(Musics_List(First_Selected_Item).Page_URL)". Then run, click on a item on the listview. Try opening the correct music page in your browser, and compare with the one showed in your app.
-
Oct 23rd, 2014, 01:56 PM
#19
Thread Starter
Lively Member
Re: Clicking Element
It is only showing my root URL..
Edit:
Just realized that when I click on the button, that is the page it is navigating to. It is actually navigating to the root URL and not another song's webpage (what I originally thought). Sorry for the confusion.
-
Oct 23rd, 2014, 02:00 PM
#20
Re: Clicking Element
Originally Posted by nyyankeesfan28
It is only showing my root URL..
Edit:
Just realized that when I click on the button, that is the page it is navigating to. It is actually navigating to the root URL and not another song's webpage (what I originally thought). Sorry for the confusion.
Can you provide HTML code of the page, so I can test here?
-
Oct 23rd, 2014, 02:01 PM
#21
Thread Starter
Lively Member
Re: Clicking Element
Of which page? The search results page??
-
Oct 23rd, 2014, 02:03 PM
#22
Re: Clicking Element
Originally Posted by nyyankeesfan28
Of which page? The search results page??
Yes, the search results page.
-
Oct 23rd, 2014, 02:04 PM
#23
Thread Starter
Lively Member
Re: Clicking Element
Originally Posted by omundodogabriel
Yes, the search results page.
I will PM it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|