Help with VB.Net HTMLInputFileElement

A

atlockhart

Hi, I am trying to create an automated web testing program that visits
a web page, fills out a form and uploads a file (from the same form in
a input type=file field). I am running into a problem when I try and
update the file input element and have read elsewhere that you can't
change it for security reasons. I have also read the work around that
says you need to grab the focus and then use sendkeys, however the
problem i'm having is with even getting a hold of the form file
element.

I keep getting the following exception in the catch block:
System.InvalidCastException: Unable to cast object of type
'mshtml.HTMLInputElementClass' to type 'mshtml.HTMLInputFileElement'.
at WebTester.CTestCase7.run()

I have tried using Ctype() to cast it to no avail.. I am pretty new to
VB, so any help would be great.. I am also open to other suggestions
as to how to post the data and file from the form to the aspx program..
thanks!

Here is my code:

Dim doc As mshtml.HTMLDocument
Dim Explorer As SHDocVw.InternetExplorer

Explorer = New SHDocVw.InternetExplorer()

Explorer.Visible = True
Explorer.Navigate("http://www.somewebsite.com/applications")

Try
doc = CType(Explorer.Document, mshtml.HTMLDocument)
Dim inputNameBox As mshtml.HTMLInputTextElement =
doc.getElementsByName("ApplicationsFirstName").item(, 0)

inputNameBox.value = "MyName"

Dim fileBox As mshtml.HTMLInputFileElement
fileBox =
doc.getElementsByName("ApplicationsFileUpload1:file").item(, 0)

Catch ex As Exception
Console.WriteLine(ex.Message())
Console.WriteLine(ex)
End Try
 
A

atlockhart

Figured it out on my own.. I wasn't reading the exception properly..
here is what I did to fix:

Dim doc As mshtml.HTMLDocument
Dim Explorer As SHDocVw.InternetExplorer

Explorer = New SHDocVw.InternetExplorer()

Explorer.Visible = True
Explorer.Navigate("http://www.somewebpage.com/applications")

'added this so that the page could finish loading
While Explorer.ReadyState <> 4

End While

Try
doc = CType(Explorer.Document, mshtml.HTMLDocument)
Dim inputBox As mshtml.HTMLInputTextElement =
doc.getElementsByName("ApplicationsFirstName").item(, 0)
inputBox.value = "Aaron"

'changed this to an InputElement rather than
InputFileElement
Dim fileBox As mshtml.HTMLInputElement
fileBox =
doc.getElementsByName("ApplicationsFileUpload1:file").item(, 0)
fileBox.focus()
My.Computer.Keyboard.SendKeys("C:\weblogs\test\test.doc",
True)



'fileBox.value = "C:\weblogs\test\test.doc"

Catch ex As Exception
Console.WriteLine(ex.Message())
Console.WriteLine(ex)
Me.WriteToLog(ex.ToString)
End Try
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top