Web browser manipulation

hi,

hi,

I am stripping few tags in html file and then assigning that html string to documenttext property of webbrowser. i can successfully do it, but when i use webbroswer1.document.GetElementById(xx) i am getting invalide type cast exception error.. can someone clear out my problem? even if i use any of webbrowser document property i am getting invalidtypecast exception..
Yogesh_AgarwalAsked:
Who is Participating?
 
Bob LearnedConnect With a Mentor Commented:
If you examine the value for "typeName" by displaying a message box, or checking in the Immediate Window with a breakpoint set, it should test you what the type is for that value.  I didn't get it right, though, since you already know that it is HtmlElementCollection.  What you are looking for is the type name for the individual elements.

That would be more like:

Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("Input")
For Each element As Object In theElementCollection
   Console.WriteLine(element.GetType().Name)
Next element

That code will write the object types to the Output window.  You can use that type name to get the correct cast for the "element" object.
0
 
Yogesh_AgarwalAuthor Commented:
anyone??
0
 
Bob LearnedCommented:
That doesn't explain enough about why you are getting that exception.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Yogesh_AgarwalAuthor Commented:
i am getting source by usiung httpwebreqest, htepwebrespnse and converting stream to string then stripping html tags and displaying it in webbrowser.. then doing manipulation like webbrowser1.document.getElementby ID(xxx).. but i am getting error only when i  use webbrowser.document properties... Error Msg = Specified type cast is not valid.
0
 
Bob LearnedCommented:
Could you show me a little code that describes this problem?
0
 
Yogesh_AgarwalAuthor Commented:
HOPE THE ABOVE CODE CLEARS U..
Dim wreq As HttpWebRequest
            Dim wres As HttpWebResponse
 
            Dim str as string ="http://www.searchenginemarketingpro.com"
            wreq = HttpWebRequest.Create(str)
            wreq.Method = "Post"
            wres = wreq.GetResponse
            Dim sr As StreamReader
            sr = New StreamReader(wres.GetResponseStream)
            str = sr.ReadToEnd
            str = replacetags(str) 'This is a function which replaces all html tags and return a string. this part is correct as it displayes correct thing in webbrowser.
            webbrowser1.DocumentText = str
            webbrowser1.update()
 
            Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("Input") 'HERE I AM GETTING INVALID TYPE CAST EXCEPTION ERROR....

Open in new window

0
 
Bob LearnedCommented:
1) If you are using the WebBrowser control, I am confused why you don't just use the WebBrowser.Navigate(url) method.

2) I like to use the Microsoft.mshtml namespace to parse the HTML, which would require the WebBrowser.Document.DomDocument.

3) Does the <input> element have an ID?
0
 
Yogesh_AgarwalAuthor Commented:
1) If you are using the WebBrowser control, I am confused why you don't just use the WebBrowser.Navigate(url) method.

############ Due to nested frames and few elements in webpage that really sucks to load, cracks the software... creates huge delay..

2) I like to use the Microsoft.mshtml namespace to parse the HTML, which would require the WebBrowser.Document.DomDocument.

########### Y cant we do it by Regex?

3) Does the <input> element have an ID?

########## its just a sample code... ya they have...
0
 
Bob LearnedCommented:
I fail to understand what the difference between using an HttpWebRequest and the WebBrowser that would cause huge delays.  If you want to use Regex to parse HTML, have fun.  

This code says to me that what you are doing should work.

HtmlDocument..GetElementsByTagName Method
http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementsbytagname.aspx
0
 
Yogesh_AgarwalAuthor Commented:
cool its nice link mate.. but i don know y its not working.. :-( getting invalid type cast exception error..
0
 
Bob LearnedCommented:
Try this:

Dim test As Object = WebBrowser1.Document.GetElementsByTagName("Input")
Dim typeName As String = test.GetType().Name
0
 
Yogesh_AgarwalAuthor Commented:
here wat is that test.gettype().Name?

i used a dead try catch finally block..

try
            Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("Input")
a:

            For Each curElement As HtmlElement In theElementCollection
************************** Codings ***************************************************

catch ex as exception
goto a
end try


he he he.. its working..
0
 
Yogesh_AgarwalAuthor Commented:
Dim test As Object = WebBrowser1.Document.GetElementsByTagName("Input")
Dim typeName As String = test.GetType().Name


mate can u please explain me this further.. how to use this?
0
 
Yogesh_AgarwalAuthor Commented:
mate my code sometimes works and sometime it doesnt.. wat will be the problem? is it timing issue?  if i set webbrowser1.documenttext= string1 then its invoking webbrowser_document completed event.. is there a way to prevent it?
0
 
Bob LearnedCommented:
I would check the WebBrowser.ReadyState if you think that it is a timing issue.
0
 
Yogesh_AgarwalAuthor Commented:
it hangs the whole application if i have  a while (webbrowser1.readystate <> webbrow.Complete)
0
 
Bob LearnedCommented:
Do you have an Application.DoEvents()?  What is the ReadyState within the While loop?
0
 
Yogesh_AgarwalAuthor Commented:
yeah i have but only stops the whole process.. :-(
0
 
Bob LearnedCommented:
Can you show me what you have so far?

I use WebBrowser.ReadyState a lot, and I have seen a few problems using it, but it is usually a web page problem.
0
 
Yogesh_AgarwalAuthor Commented:
mate instead of using that thing(readystate and loop) can i use webbrowswer1.update() to load the page completely? most of problem that i have encountered is timing issue..
0
 
Yogesh_AgarwalAuthor Commented:
good..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.