Internet Explorer DOM

I’m trying to learn how to work with IE DOM objects such as buttons and text area in powershell. What I’m trying to do is enter value in the input, and click the button from this website

This is what I have so far, but I’m getting errors. The elementid of the input is “autocomplete”, and the id for button is “btnsearch”.

$browser=new-object -com internetexplorer.application
$search = $browser.Document.getElementById("autocomplete")
$button = $browser.Document.getElementById("btnSearch")

The is a module on CodePlex you can use that might save you this work.

It is open source, so you can see how things are done and tweak to your needs.
It was last published in 2009, so a bit old, but still works.

Ok so, if you wanna learn this, it helps if you have some prior knowledge of the DOM-object with HTML and Javascript.

So you should look into get-member a bit more.
If you would use:

$browser.Document.getElementById("btnSearch") | gm

It will show you that you have a click() method.
So we can use that to invoke our click-event.

Now notice that i have a while{} included. This is because without it, you would invoke the click event before the page is fully loaded. This would end up not working and throwing you an error-message.

If you ever have worked with Javascript , or with a framework like Jquery, you would go like:

    $(document).ready(function(){ /* .click fucntion over here. */ });

This would wait until the page had loaded before triggering the Click-event.

    $browser=new-object -com internetexplorer.application
    $browser.visible = $true
    while ($browser.busy){Start-Sleep -Milliseconds 500}
    $browser.Document.getElementById("autocomplete").value = "LATI"

If you don’t work with the new-object but with Invoke-webrequest, you don’t have this issue.
I don’t know why that is, i figure it probably has a build in wait function.

So your code would look like this:

    $payload = Invoke-WebRequest ""
    $inputfield = $payload.ParsedHtml.getElementById('autocomplete').value = "LATI"