Home Dashboard Directory Help
Search

Invoke-WebRequest response.forms.fields uses Id attribute as the key instead of Name by Aaron Stringer


Status: 

Active


3
0
Sign in
to vote
Type: Bug
ID: 775739
Opened: 1/3/2013 3:32:53 PM
Access Restriction: Public
1
Workaround(s)
view
2
User(s) can reproduce this bug

Description

When an <input> tag in an html form has an "id" attribute as well as "name" the "id" field is used as the key in the response.forms.fields collection.
When this collection is used in a POST as per the technet documentation the values are not recognised by the web server as the names are not correct. ("http://technet.microsoft.com/en-us/library/hh849901.aspx" Exampe 2)

HTML contains:
        <input type="text" name="username" id="login_username" class="field required" title="Please provide your username" />

$Response = Invoke-WebRequest http://fakeurl.com/login.aspx
$Response.Forms[0].Fields

Fields should contain the key "username" but instead contains the key "login_username"
Details
Sign in to post a comment.
Posted by Ronald Rink on 5/10/2013 at 11:59 PM
I experience the same behaviour. You can however correlate the form names and ids via the InputFields collection as shown in the 'Workarounds' tab.
Posted by Chris Hunnewell on 2/28/2013 at 3:04 PM
I experienced this same issue on a site that contained the following form inputs:
<input id="username" type="text" name="__ac_name" class="inputbox" style="top:160px" />
<input type="password" name="__ac_password" class="inputbox" style="top:202px;" />

$Response.Forms[0].Fields contained the following two keys:
username
__ac_password
Sign in to post a workaround.
Posted by Ronald Rink on 5/11/2013 at 12:00 AM
Take the webresponse from Invoke-WebRequest and lookup the names of the input id from the WebResponse.InputFields collection and replace the Field collection of the form you are interested in:$
function ConvertTo-InputIdFromFieldName($wr, $string) {
    return ($wr.InputFields | Where { $_.name -eq $string }).id;
}

function ConvertTo-FieldNameInputIdFrom($wr, $string) {
    return ($wr.InputFields | Where { $_.id -eq $string }).name;
}
$f = $wr.Forms.Item('SomeForm');
$ht = @{};
foreach($field in $f.Fields.GetEnumerator()) {
    $fieldName = $null;
    $fieldName = ConvertTo-FieldNameInputIdFrom $wr $field.Key;
    "{0}`t{1}`t{2}" -f $field.Key, $fieldName, $field.Value;
    if($fieldName) { $ht.Add($fieldName, $field.Value); }
} # foreach
$f.Fields.Clear();
foreach($e in $ht.GetEnumerator()) {
    #if($f.Fields.Contains($e.Name) { $f.Fields.Remove($e.Name); }
    $f.Fields.Add($e.Name, $e.Value);
} # foreach

Check http://d-fens.ch/2013/05/11/invoke-webrequest-uses-id-attribute-of-input-element-as-field-name-in-form-fields-collection/ for a more readable explanation.