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
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
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" ?
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
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")
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
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
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"
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.
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> </span><div class="btn3">Like</div></a>
What would you do in this instance?
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.
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.
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
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:
Dim htmldoc = AxWebBrowser1.Document ' is there a reason for not using the more up to date control?
' Dim idstring As String
Dim pulledtags = htmldoc.getelementsbytagname("A")
For Each item In pulledtags
If item.innertext = "Like" Then
' idstring = item.id
item.InvokeMember("onclick") ' you may get better success with CType(item, HtmlElement).InvokeMember("onclick")
End If
' Dim a = htmldoc.getelementbyid("idstring")
' a.invokemember("onclick")
Next
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
?
Re: Click Button in WebBrowser Control with changing ID
Quote:
Originally Posted by
johnwillmikul
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
Re: Click Button in WebBrowser Control with changing ID
Yeah I would, but there is no ID :P
Re: Click Button in WebBrowser Control with changing ID
Quote:
Originally Posted by
johnwillmikul
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.
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.
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.
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:
/(|
( :
__\ \ _____
(____) `|
(____)| |
(____).__|
(___)__.|_____