Login onto webpage: getElementsByTagName and Windows Server 2012 R2

This topic contains 1 reply, has 2 voices, and was last updated by  Don Jones 1 year, 10 months ago.

  • Author
  • #31911


    Im trying to logon to a web page, go to a page, modify a value, and save it.

    There are two ways to do this: "$ie = New-Object -com internetexplorer.application;" OR "Invoke-WebRequest". I perfer to do the way that is more clear to me.

    Trying to do it thru "$ie = New-Object -com internetexplorer.application", I get a error when trying $ie.Document.getElementsByName as in:

    Cannot find an overload for "getElementsByTagName" and the argument count: "1".

    This only happens in Windows Server 2012 R2, not in Windows 7.

    I havent played around too much with Invoke-WebRequest as Im not too sure how to do it.

    I had this for now:

    $initialRequest = Invoke-WebRequest -Uri '' -SessionVariable WebSession1

    form = $initialRequest.Forms[0]
    $form.fields.userpwd = "mypass"
    $form.fields.username = "myuser"

    $loginRequest = Invoke-WebRequest -Uri ('' + $form.Action) -Method Post -Body $form.fields -WebSession $WebSession1

    $tcpipRequest = Invoke-WebRequest -Uri '' -WebSession $WebSession1

    # TODO: After this, I want to first test going to I need to go

    # TODO: Then I need to get the text value inside a textbox called gateway

    So how do I keep going?

  • #31931

    Don Jones

    The IE COM object is imperfectly mapped into .NET by Interop, meaning it isn't always reliable – as you're seeing. And, most importantly, it doesn't exist on most servers as it does on clients – which is WHY you're seeing what you are.

    The difficulty is that Invoke-WebRequest isn't a web browser. It doesn't implement JavaScript, and it doesn't do all the magic that a browser does. If you already KNOW the form fields that you want to submit, there's no reason to retrieve the page whatsoever. You simply have to POST a request to the web page (using Invoke-WebRequest), and include your form field values in the request header.

    This is going to require quite a bit of familiarity with how the HTTP protocol works.

You must be logged in to reply to this topic.