?
Solved

Web browser manipulation

Posted on 2009-04-19
21
Medium Priority
?
621 Views
Last Modified: 2013-12-19
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..
0
Comment
Question by:Yogesh_Agarwal
  • 12
  • 9
21 Comments
 

Author Comment

by:Yogesh_Agarwal
ID: 24179083
anyone??
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24184406
That doesn't explain enough about why you are getting that exception.
0
 

Author Comment

by:Yogesh_Agarwal
ID: 24185289
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 24185329
Could you show me a little code that describes this problem?
0
 

Author Comment

by:Yogesh_Agarwal
ID: 24185886
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24187736
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
 

Author Comment

by:Yogesh_Agarwal
ID: 24194200
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24194268
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
 

Author Comment

by:Yogesh_Agarwal
ID: 24194452
cool its nice link mate.. but i don know y its not working.. :-( getting invalid type cast exception error..
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24194548
Try this:

Dim test As Object = WebBrowser1.Document.GetElementsByTagName("Input")
Dim typeName As String = test.GetType().Name
0
 

Author Comment

by:Yogesh_Agarwal
ID: 24194642
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
 

Author Comment

by:Yogesh_Agarwal
ID: 24213494
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
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1500 total points
ID: 24214099
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
 

Author Comment

by:Yogesh_Agarwal
ID: 24214262
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24217766
I would check the WebBrowser.ReadyState if you think that it is a timing issue.
0
 

Author Comment

by:Yogesh_Agarwal
ID: 24223122
it hangs the whole application if i have  a while (webbrowser1.readystate <> webbrow.Complete)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24225136
Do you have an Application.DoEvents()?  What is the ReadyState within the While loop?
0
 

Author Comment

by:Yogesh_Agarwal
ID: 24226589
yeah i have but only stops the whole process.. :-(
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24227573
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
 

Author Comment

by:Yogesh_Agarwal
ID: 24230825
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
 

Author Closing Comment

by:Yogesh_Agarwal
ID: 31571944
good..
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
The purpose of this video is to demonstrate how to properly insert a Vimeo Video into a WordPress site or Blog. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question