This will set the value, but will not cause a postback:
function SelectValue($ddl, $val) {
$ddl.options | % {$i = 0}{ if ($_.value -eq $val) {
$ddl.selectedIndex = $i; }; $i += 1; }
}
Also, once I get the postback working, will I need to add a delay, eg:
while($ie.ReadyState -ne 4) {
start-sleep -m 100
}
The HTML looks something like this:
<select name="ddl" onchange="__doPostBack('ddl','')" language="javascript" id="ddl" class="required" style="width:100%;">
<option value="-- Please Select --">-- Please Select --</option>
<option value="Some Value">Some Value</option>
</select>
Edit: Why would I want to do this in Powershell? Probably WatiN would be a lot simpler (though I've never used WatiN). But I'm just trying to figure out how to use Powershell to do quick, ad hoc, limited web site automation. I travel a lot and use different machines, and Powershell is one of the few constants that I can depend on.
Solution: This turned out to be easy and it's quite useful because now I can literally script a page to come up, make a few selections, and click a submit button, as if by magic, and it's EASY. I think my love/hate relationship with Powershell is turning back to love. Anyway the key is to use the form submit instead of using the onchange event, which is known not to fire when invoked with javascript anyway. Here is how to do it:
$frm = $ie.document.getElementById("frm");
$frm.submit();
I found that i did not have to wait for postbacks. For completeness, clicking a button is just like it is in javascript:
$btnReport = $ie.document.getElementById("btnReport")
$btnReport.click()