The code works only in half

Arjeen

Regular Member
Joined
Aug 22, 2012
Messages
294
Reaction score
16
Hei:) i'm finishing my program (sorry for newbie) but there is one problem..
The program have to search the sentence on the ListBox1 and put in the WebBrowser, search and do it again, again, again, again..
But only for the first ListBox1.SelectItem work.. I don't know why..

This is my video problem:

http://www.youtube.com/watch?v=HUzvsq8Txq0


And this is my code:
Code:
 Private Sub ElectricButton2_Click(sender As Object, e As EventArgs) Handles ElectricButton2.Click        WebBrowser1.Navigate("https://www.blurum.it/Web/")
        Dim searchBox As HtmlElement = Nothing
        Dim searchButton As HtmlElement = Nothing
        searchBox = WebBrowser1.Document.GetElementsByTagName("input").OfType(Of HtmlElement)().FirstOrDefault(Function(p) p.GetAttribute("classname") = "search_bar_input")
        searchButton = WebBrowser1.Document.GetElementsByTagName("input").OfType(Of HtmlElement)().FirstOrDefault(Function(p) p.GetAttribute("classname") = "search")
        Do While WebBrowser1.Document Is Nothing OrElse searchBox Is Nothing OrElse searchButton Is Nothing
            Application.DoEvents()
            searchBox = WebBrowser1.Document.GetElementsByTagName("input").OfType(Of HtmlElement)().FirstOrDefault(Function(p) p.GetAttribute("classname") = "search_bar_input")
            searchButton = WebBrowser1.Document.GetElementsByTagName("input").OfType(Of HtmlElement)().FirstOrDefault(Function(p) p.GetAttribute("classname") = "search")
        Loop
        Dim keyword As String = ListBox1.SelectedItem
        searchBox.SetAttribute("value", keyword)
        searchButton.InvokeMember("click")
        WebBrowser1.Refresh()
        For i As Integer = ListBox1.SelectedItems.Count - 1 To 0 Step -1
            Dim k As Integer = ListBox1.SelectedIndices(i)
            ListBox1.Items.RemoveAt(k)
        Next
        Timer1.Enabled = True
    End Sub






    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Timer1.Enabled = True
        WebBrowser1.Navigate("https://www.blurum.it/Web/")
        Dim searchBox As HtmlElement = Nothing
        Dim searchButton As HtmlElement = Nothing
        searchBox = WebBrowser1.Document.GetElementsByTagName("input").OfType(Of HtmlElement)().FirstOrDefault(Function(p) p.GetAttribute("classname") = "search_bar_input")
        searchButton = WebBrowser1.Document.GetElementsByTagName("input").OfType(Of HtmlElement)().FirstOrDefault(Function(p) p.GetAttribute("classname") = "search")
        Do While WebBrowser1.Document Is Nothing OrElse searchBox Is Nothing OrElse searchButton Is Nothing
            Application.DoEvents()
            searchBox = WebBrowser1.Document.GetElementsByTagName("input").OfType(Of HtmlElement)().FirstOrDefault(Function(p) p.GetAttribute("classname") = "search_bar_input")
            searchButton = WebBrowser1.Document.GetElementsByTagName("input").OfType(Of HtmlElement)().FirstOrDefault(Function(p) p.GetAttribute("classname") = "search")
        Loop
        Dim keyword As String = ListBox1.SelectedItem
        searchBox.SetAttribute("value", keyword)
        searchButton.InvokeMember("click")
        WebBrowser1.Refresh()
        For i As Integer = ListBox1.SelectedItems.Count - 1 To 0 Step -1
            Dim k As Integer = ListBox1.SelectedIndices(i)
            ListBox1.Items.RemoveAt(k)
        Next
        Dim asd = ListBox1.Items(0)
        WebBrowser1.Document.GetElementsByTagName("input").OfType(Of HtmlElement)().FirstOrDefault(Function(p) p.GetAttribute("classname") = "search_bar_input").InnerText = (asd)
        ListBox1.Items.RemoveAt(0)
        Dim r as New Random
        Timer1.Interval = r.Next(30000, 60000)
        Timer1.Enabled = True
    End Sub
 
thanks for the help
 
Variable 'keyword' only returns the currently selected item (which will probably just be the first item). A better way to do what you are trying to accomplish would be to create a queue, and load the queue with your selected values. Then use the queue to set 'Keyword'
 
this isn't the problem bro
 
You still didn't solve your problem ?
Can't you see that there are two inputs ?
Code:
<div class="search_bar_style">
                    <input name="ctrl_SearchControl$txtSearch" type="text" maxlength="255" id="txtSearch" class="search_bar_input" style="display:none;">
                    <input name="ctrl_SearchControl$A2350807d-11cb-41de-ac2f-ed36857e9d30" type="text" value="test" maxlength="255" id="A2350807d-11cb-41de-ac2f-ed36857e9d30" class="search_bar_input">
                </div>
Set your keyword to both of them.
 
But mine are true or no? Becouse the first search is correct, and other no..
 
The problem may be that using a timer to automate a webbrowser control is crossthreading. Try using the second part of your code in a document completed event. Could also be that you are using the selected item from a listbox to populate your 'keyword' variable, then removing that item from the listbox, possibly leaving no item selected. Use a loop or a queue to step through your keywords.
 
Back
Top