views:

1013

answers:

2

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()
+1  A: 

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.

Yes, you should use WatiN, but you want to use PowerShell? So use WatiN from PowerShell!

PowerShell is a general shell/scripting/automation tool. In order to get things done, PowerShell utilizes specialed tools (COM, WMI, .NET, etc.).

Here is something to get started with.

http://poshcode.org/1108

JasonMArcher
A: 

You can use iMacros, it exposes a COM object and thus can be used directly from Powershell: http://wiki.imacros.net/PowerShell

Only disadvantage: Its not free. For a quick and easy free solution try the iMacros Firefox addon. It can also be used via the command line: http://wiki.imacros.net/iMacros_for_Firefox#Command_Line_Support

Tim2010