Internet Explorer DOM

This topic contains 2 replies, has 3 voices, and was last updated by  Snak3d0c 3 weeks ago.

  • Author
    Posts
  • #80366

    Tony Antony
    Participant

    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 https://www.aviationweather.gov/adds/

    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
    $browser.Navigate("https://www.aviationweather.gov/adds/")
    $search = $browser.Document.getElementById("autocomplete")
    $button = $browser.Document.getElementById("btnSearch")
  • #80413

    postanote
    Participant

    The is a module on CodePlex you can use that might save you this work.
    'wasp.codeplex.com'

    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.

  • #80467

    Snak3d0c
    Participant

    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.Navigate2("https://www.aviationweather.gov/adds/")
        $browser.visible = $true
        while ($browser.busy){Start-Sleep -Milliseconds 500}
        $browser.Document.getElementById("autocomplete").value = "LATI"
        $browser.Document.getElementById("btnSearch").click()
    

    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 "https://www.aviationweather.gov/adds/"
        $payload
        $submitButton=$payload.ParsedHtml.getElementById('btnSearch')
        $inputfield = $payload.ParsedHtml.getElementById('autocomplete').value = "LATI"
        $submitButton.click()
    

You must be logged in to reply to this topic.