Link to home
Start Free TrialLog in
Avatar of Yogesh_Agarwal
Yogesh_Agarwal

asked on

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..
Avatar of Yogesh_Agarwal
Yogesh_Agarwal

ASKER

anyone??
Avatar of Bob Learned
That doesn't explain enough about why you are getting that exception.
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.
Could you show me a little code that describes this problem?
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

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?
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...
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
cool its nice link mate.. but i don know y its not working.. :-( getting invalid type cast exception error..
Try this:

Dim test As Object = WebBrowser1.Document.GetElementsByTagName("Input")
Dim typeName As String = test.GetType().Name
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..
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?
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
I would check the WebBrowser.ReadyState if you think that it is a timing issue.
it hangs the whole application if i have  a while (webbrowser1.readystate <> webbrow.Complete)
Do you have an Application.DoEvents()?  What is the ReadyState within the While loop?
yeah i have but only stops the whole process.. :-(
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.
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..
good..