Click Button in WebBrowser Control with changing ID-VBForums
Results 1 to 21 of 21

Thread: Click Button in WebBrowser Control with changing ID

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2013
    Posts
    40

    Click Button in WebBrowser Control with changing ID

    If I had a button with the ID of

    id="fbSingle_43b9a20f6f0e6bdcae16bc23192083"

    But besides the line "fbSingle_" Everything else changes upon the page refresh, how could I click the button using this code-->

    Code:
    WebBrowser1.Document.All("Button ID").InvokeMember("click")
    But instead of finding the exact ID, the webbrowser looks up if the Document has an ID containing a certain string. Almost like how in a textbox you would use

    Code:
    If Textbox1.Text.Contains("fbSingle_") Then
    'Do Something
    End If
    Thank you all

    Also,
    if you know how to click a button by the text on the button, that would be very helpful. The button I am trying to click is
    Code:
    <div class="btn3">Like</div>
    Though if you open up the XPath, it is
    Code:
    //*[@id="fbSingle_43b9a20f6f0e6bdcae16bc23192083"]/div/center[2]/a/div
    Last edited by johnwillmikul; Mar 8th, 2013 at 04:36 PM. Reason: New Info for better answers

  2. #2
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,173

    Re: Click Button in WebBrowser Control with changing ID

    you would have to determine how to get the id, probably based on the tag name value first, test if it contains an id or a known value and then click it, assuming you can get both of those requirements together. The last question, you would get the elements by tag name and test if the div tag class = "btn3" then somehow try to click it. However, I see this could be being used for devious means, im not going to code it for you
    Last edited by jayinthe813; Mar 8th, 2013 at 07:09 PM.

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2013
    Posts
    40

    Re: Click Button in WebBrowser Control with changing ID

    What do you mean by devious means...lol...I'm not trying to do anything bad. And by the 'elements', do you mean "div" or "button" ?

  4. #4
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,173

    Re: Click Button in WebBrowser Control with changing ID

    it becomes a problem of narrowing down your search parameters essentially. pull all the elements by the div tag (.getelementsbytagname) then you need to look at each item in that collection and test for a known value such as a button text .value="click" for instance, or try testing for an "ID" so theoretically it could look like:
    Code:
    dim idstring as string
    dim pulledtags = htmldoc.getelementsbytagname("DIV")
    
    for each item in pulledtags
    if item.id <> nothing and item.value = "Click Me" then
    
    idstring = item.id
    
    next
    Thats off the top of my head, it would have to be reworked

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2013
    Posts
    40

    Re: Click Button in WebBrowser Control with changing ID

    Yes, but where did you get the "click me" value from? Could you classify it using the text? ("Like")

  6. #6
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,173

    Re: Click Button in WebBrowser Control with changing ID

    in the case of "like" that would be innertext:

    Code:
    if item.id <> nothing and item.innertext = "Like" then
    
    idstring = item.id
    
    end if
    
    htmldoc.getelementbyid("idstring").invokeMember("click")
    you need to identify your elements based on what you are given, so it can change depending on your context. Try adding a watch and looking for a unique element that will identify your button each time
    Last edited by jayinthe813; Mar 8th, 2013 at 11:44 PM.

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2013
    Posts
    40

    Re: Click Button in WebBrowser Control with changing ID

    Ok, it seems like the code is finding the button, but at the click command I get the error:

    Null Reference Exception was unhandled
    Object variable or With block variable not set.

    Do you know how to fix this?
    It prevents the button from being clicked

  8. #8
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,173

    Re: Click Button in WebBrowser Control with changing ID

    try adding a watch to A:

    dim a = htmldoc.getelementbyid("idstring")

    and then see what A is.

    then in your code:
    a.click()
    a.invokemember("click")

    you got that error because the program found nothing to click. That could get tricky as it may not be a standard "button"

  9. #9
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Click Button in WebBrowser Control with changing ID

    I don't think there's any doubt that it's not a standard button. In fact I'm inclined to doubt that this is the 'active' part of the source at all. If the OP is unwilling or unable to reveal the url for scrutiny of the full page at least some more of the HTML source is needed to determine exactly what we're dealing with here.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  10. #10

    Thread Starter
    Member
    Join Date
    Mar 2013
    Posts
    40

    Re: Click Button in WebBrowser Control with changing ID

    Yeah, it seems like the html of the "btn3" above wasn't right. This must've been the code
    Code:
    <a class="single_like_button btn3-wrap" onclick="openFbLWin_50741();">
    <span>&nbsp;</span><div class="btn3">Like</div></a>
    What would you do in this instance?

  11. #11
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,173

    Re: Click Button in WebBrowser Control with changing ID

    you would have to use .invokemember("onclick") or fireEvents if using mshtml:

    Code:
    element = htmldoc.getElementById(elemName)
    element.FireEvent("onclick")
    it seems you are attempting to click facebook like buttons. I would think facebook would make strides to counter programmatic clicking of their buttons so it may be futile in the end for you if these last few methods do not work.
    Last edited by jayinthe813; Mar 9th, 2013 at 11:59 AM.

  12. #12
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Click Button in WebBrowser Control with changing ID

    That makes more sense. In this case you need to isolate the <a> tag (using attribute "classname" possibly or the inner text "Like") and invoke the click on that. If "click" doesn't work, try "onclick". If neither work then I'm afraid you just may have to accept that nothing will.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  13. #13

    Thread Starter
    Member
    Join Date
    Mar 2013
    Posts
    40

    Re: Click Button in WebBrowser Control with changing ID

    So...is this the correct code? Because it's not working, I might be doing something wrong though.

    Code:
     Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            Dim htmldoc = AxWebBrowser1.Document
            Dim idstring As String
            Dim pulledtags = htmldoc.getelementsbytagname("DIV")
            For Each item In pulledtags
                If item.id <> Nothing And item.innertext = "Like" Then
                    idstring = item.id
                End If
                Dim a = htmldoc.getelementbyid("idstring")
                a.invokemember("onclick")
            Next
        End Sub

  14. #14
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Click Button in WebBrowser Control with changing ID

    No, you're still pulling the div element when it should be the a element. And also you're using an ID that simply isn't there.

    vb.net Code:
    1. Dim htmldoc = AxWebBrowser1.Document ' is there a reason for not using the more up to date control?
    2.         ' Dim idstring As String
    3.         Dim pulledtags = htmldoc.getelementsbytagname("A")
    4.         For Each item In pulledtags
    5.             If item.innertext = "Like" Then
    6.               '  idstring = item.id
    7.               item.InvokeMember("onclick") ' you may get better success with CType(item, HtmlElement).InvokeMember("onclick")
    8.             End If
    9.             ' Dim a = htmldoc.getelementbyid("idstring")
    10.            ' a.invokemember("onclick")
    11.         Next
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  15. #15

    Thread Starter
    Member
    Join Date
    Mar 2013
    Posts
    40

    Re: Click Button in WebBrowser Control with changing ID

    Oh, and sorry about the AxWebBrowser, just when I used a regular WebBrowser I would get an error on WebBrowser1.Document.All. I would prefer the up to date WebBrowser code though. Is it the same, but line 1 is
    Code:
    Dim htmldoc = WebBrowser1.Document.All
    ?

  16. #16
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,173

    Re: Click Button in WebBrowser Control with changing ID

    Quote Originally Posted by johnwillmikul View Post
    Oh, and sorry about the AxWebBrowser, just when I used a regular WebBrowser I would get an error on WebBrowser1.Document.All. I would prefer the up to date WebBrowser code though. Is it the same, but line 1 is
    Code:
    Dim htmldoc = WebBrowser1.Document.All
    ?
    you are dimensioning htmldoc incorrectly.
    to use the regular browser control its:

    Code:
    dim htmldoc as mshtml.htmldocument
    htmldoc = webbrowser1.document.domdocument
    you cant directly make htmldoc equal directly to the .net browser document. keep in mind you can also do

    Code:
    webbrowser1.document.getelementbyid()
    which will mean that for some things you could eliminate html object library entirely and use the .net component

  17. #17

    Thread Starter
    Member
    Join Date
    Mar 2013
    Posts
    40

    Re: Click Button in WebBrowser Control with changing ID

    Yeah I would, but there is no ID :P

  18. #18
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,173

    Re: Click Button in WebBrowser Control with changing ID

    Quote Originally Posted by johnwillmikul View Post
    Yeah I would, but there is no ID :P
    Ok, but theres webbrowser1.document.getelementsbytagname()

    I have found though that it is lacking .getelementsbyname() so I sometimes still end up using htmldoc just because its less narrowing down for me to do.

  19. #19

    Thread Starter
    Member
    Join Date
    Mar 2013
    Posts
    40

    Re: Click Button in WebBrowser Control with changing ID

    dunfiddlin, thank you for the code, but it doesn't really seem to do anything, no error, no click.

  20. #20
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Click Button in WebBrowser Control with changing ID

    Sorry bout that but, as I said, there are no guarantees. A lot of websites are now actively taking measures to reduce, if not eliminate, automation (for obvious reasons given the potential for abuse). Sometimes you just gotta say 'Shucks, you got me!' and accept defeat.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  21. #21

    Thread Starter
    Member
    Join Date
    Mar 2013
    Posts
    40

    Re: Click Button in WebBrowser Control with changing ID

    Lol ok. I guess that's a good idea. I was able to click it in iMacros for firefox...well whatever. I guess it is time to just give up. Thanks for all the help though I really appreciated it .
    Code:
         
               /(|
              (  :
             __\  \  _____
           (____)  `|
          (____)|   |
           (____).__|
            (___)__.|_____

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.