views:

679

answers:

3

I have a input type=text inputboxes on a webpage i load and fill with values and click the submitbutton which works fine:

$ie=New-Object -comobject InternetExplorer.Application 
$ie.visible=$true 
$ie.Navigate("https://myurl/test.html") 
while($ie.busy){Start-Sleep 1} 
$ie.Document.getElementById("field_firstName").value="Firstname" 
$ie.Document.getElementById("field_lastName").value="Lastname" 
$ie.Document.getElementById("btn_upload").Click() 
while($ie.busy){Start-Sleep 1}

I'd also like to populate a input type=file box with c:\temp\test.txt and upload this. I read that because of security reasonse the value= is not supported from the browsers.

Is there any workaround to do this with powershell? Maybe "click" the browse button and select the file or use sendkey's ?

A: 

I would strongly consider using WebClientor HttpWebRequest instead of driving the IE GUI. There is a WebClient example using PowerShell.

Matthew Flaschen
Thanks for response, Matthew. Although i'd prefer IE Gui at the moment as i can see the results then and avoid problems with proxy's certificates etc.. i would be intrested how the posted code above would look like with a webclient approach..
icnivad
It looks like you may not be able to do what you want with pure WebClient. There is a function at CodeProject (http://www.codeproject.com/KB/cs/uploadfileex.aspx) you could look at. It's basically a more sophisticated version of WebClient.UploadFile, based on HttpWebRequest.
Matthew Flaschen
+1  A: 

Check Jaykul's post. He uses Watin for automation. Just download the assembly and try. I was able to set the value and then submit the form like this:

$WatinPath = 'c:\bin\watin\WatiN.Core.dll' #path with downloaded assembly
$watin     = [Reflection.Assembly]::LoadFrom( $WatinPath )

$ie        = new-object WatiN.Core.IE("https://myurl/test.html")
$file1 = $ie.FileUpload('file1') #id of the input
$file1.set('C:\temp\test.txt') # path to the file

# and now just find the button and click on it
$o = $ie.Button('send') #send is id of the submit button
$o.Click()

I understand your reasons to use IE instead of WebClient and similar classes, however use them in other cases if possible.

Edit:

In case you don't have ID of the element, but only the name, you can try

$f = $ie.FileUpload({param($fu) $fu.GetAttributeValue("name") -eq 'the name you have' })

or

$f = $ie.FileUploads | ? { $_.GetAttributeValue("name") -eq 'the name you have' }
stej
This looks good, although i have Problems to find the correct field as the source has no id but just a name... Do you know if i can search by name and not by id?
icnivad
I used your example where you call function getElementById, so I thought you know the id. I'll check if it is possible.
stej
I am also confused why my first example worked bacause there i also just have a name and no id tag...With your last example $_.GetAttributeValue("name") it seems that i find the box but my submitbutton (also no id just name) didn't worked. Have to play with that again tommorrow. Thank you so far
icnivad
You have to find the submit button similarly like the file upload: `$sub = $ie.Button({param($fu) $t.GetAttributeValue("name") -eq 'name of submit button' })` I don't know it exactly, I cann't examine it now. Just try `$ie | gm` and you will find the right method ;)
stej
This worked! Thanks!In the watin lists i found:ie.FileUpload(Find.ByName("ctl02$myfile")).Set("test.txt)But i couldn't get the Find.ByName working.. I'll yous your suggested solution now.
icnivad
A: 

Yes, you would have to use the SendKeys route if you want to do this in IE. Direct text-entry into the box is blocked in IE8 and higher.

EricLaw -MSFT-