1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Clicking html element loaded by javascript (jquery live)

Discussion in 'Visual Basic .NET' started by Kalashnikov, Apr 29, 2012.

  1. Kalashnikov

    Kalashnikov Junior Member

    Joined:
    Feb 18, 2011
    Messages:
    148
    Likes Received:
    59
    Location:
    Northern Ireland
    I'm having trouble with my bot, I'm trying to make it click an element that's loaded by javascript but it's not appearing in the elements list.

    Any workarounds?
     
  2. Chris22

    Chris22 Regular Member

    Joined:
    Sep 29, 2010
    Messages:
    400
    Likes Received:
    1,060
    Are you using a webbrowser control?

    If so maybe you could trigger it by setting the address as something like this:
    Code:
    javascript: $("#item").trigger('click'); void 0;
     
    • Thanks Thanks x 1
  3. saninfotech

    saninfotech Regular Member

    Joined:
    Apr 22, 2012
    Messages:
    217
    Likes Received:
    52
    Occupation:
    Onliine Internet Marketing
    Location:
    BHW
    Home Page:
    may i know your full details???
     
    • Thanks Thanks x 1
  4. roster67

    roster67 Registered Member

    Joined:
    Mar 27, 2012
    Messages:
    69
    Likes Received:
    10
    You should wait until window onload event is completed.
    Maybe a thread sleep, but as we dont have any details about your bot, we cannot help you more.
     
    • Thanks Thanks x 1
  5. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,513
    Likes Received:
    10,453
    @Chris22: A gotcha here is that $().trigger() does not work on events that where not binded by use of jQuery ;)
     
    • Thanks Thanks x 1
  6. Chris22

    Chris22 Regular Member

    Joined:
    Sep 29, 2010
    Messages:
    400
    Likes Received:
    1,060
    Ah, I sort of suspected so, but OP mentioned jQuery live so I figured it could be worth a shot.
     
  7. Kalashnikov

    Kalashnikov Junior Member

    Joined:
    Feb 18, 2011
    Messages:
    148
    Likes Received:
    59
    Location:
    Northern Ireland
    Here's what I'm attempting to do, you see the blue comment bar:

    Code:
    soundcloud.com/tenaciousd/low-hangin-fruit
    
    I tried clicking it via my bot for ages before searching through their javascript file and finding that it was loaded live, any ideas?

    Currently my bot works flawlessly with the non timed comments but if I want to reach a bigger audience I need timed comments.

    Any help would be appreciated.
     
    Last edited: Apr 30, 2012
  8. roster67

    roster67 Registered Member

    Joined:
    Mar 27, 2012
    Messages:
    69
    Likes Received:
    10
    But what kind of bot are you using?
    Have you got source code?
    Can you execute javascript on page?
     
  9. Kalashnikov

    Kalashnikov Junior Member

    Joined:
    Feb 18, 2011
    Messages:
    148
    Likes Received:
    59
    Location:
    Northern Ireland
    Web browser bot, I've tried before without luck. If anyone could attempt to click the blue bar via a vb.net browser bot I'd appreciate it.
     
  10. roster67

    roster67 Registered Member

    Joined:
    Mar 27, 2012
    Messages:
    69
    Likes Received:
    10

    This works for me: {sorry c#}

    Code:
      void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath)
                    return; 
                HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
                HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
                IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
                element.text = "function clickIt() { $('.timestamped-comments:first').click(); }";
                head.AppendChild(scriptEl);
                webBrowser1.Document.InvokeScript("clickIt");
            }
    
     
  11. roster67

    roster67 Registered Member

    Joined:
    Mar 27, 2012
    Messages:
    69
    Likes Received:
    10
    Ok this vb versio:

    Code:
    
    Private Sub webBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)  
      If e.Url.AbsolutePath <> TryCast(sender, WebBrowser).Url.AbsolutePath
     Then        Return    
    End If    
    Dim head As HtmlElement = webBrowser1.Document.GetElementsByTagName("head")(0)
    Dim scriptEl As HtmlElement = webBrowser1.Document.CreateElement("script")    
    Dim element As IHTMLScriptElement = DirectCast(scriptEl.DomElement, IHTMLScriptElement)    
    element.text = "function clickIt() { $('.timestamped-comments:first').click(); }"
    head.AppendChild(scriptEl)    
    webBrowser1.Document.InvokeScript("clickIt")
    End Sub
     
  12. Kalashnikov

    Kalashnikov Junior Member

    Joined:
    Feb 18, 2011
    Messages:
    148
    Likes Received:
    59
    Location:
    Northern Ireland
    I can't seem to get the converted code to work in VB.. IHTMLScriptElement is undefined
     
  13. roster67

    roster67 Registered Member

    Joined:
    Mar 27, 2012
    Messages:
    69
    Likes Received:
    10
    You need to import mshtml

    It is:
    Code:
    [/B][B]mshtml.[/B][COLOR=#FFFFCC]IHTMLScriptElement[/COLOR][B]
     
    Last edited: Apr 30, 2012
  14. Kalashnikov

    Kalashnikov Junior Member

    Joined:
    Feb 18, 2011
    Messages:
    148
    Likes Received:
    59
    Location:
    Northern Ireland
    I see, my vb is crashing adding the reference. I'll try and fix it and get back to you.
     
  15. roster67

    roster67 Registered Member

    Joined:
    Mar 27, 2012
    Messages:
    69
    Likes Received:
    10
    So, working now?
     
  16. Kalashnikov

    Kalashnikov Junior Member

    Joined:
    Feb 18, 2011
    Messages:
    148
    Likes Received:
    59
    Location:
    Northern Ireland
    Still can't add the reference, seems to be an issue with newer operating systems..