powershellinternet-explorercomobject

How can I Navigate to a link after login using ComObject?


How can I navigate to a subsite using a ComObject

Add-Type -AssemblyName System.Windows.Forms
$woop=New-Object -ComObject 'internetExplorer.Application'
$woop.Visible= $true 
$woop.Navigate("Link")
Start-Sleep 5;
$woop.document.IHTMLDocument3_getElementByID("Username-ID").value=$username 
$woop.document.IHTMLDocument3_getElementByID("Password-ID").value=$password 
$woop.document.IHTMLDocument3_getElementByID("Loggin Button").Click()
Start-Sleep 20 #20 Second timer to wait for the page to fully load
$woop.document.IHTMLDocument3_getElementByID("NewSiteContent-ID").Click()

This Code works like a charm for me and takes me to the new subsite. However after I log in to the Site, I can't .Navigate to this new Site with the ComObject since I don't have the session stored anywhere.

My question: does this problem even have a solution without using Invoke Request?

I found a very good guide for this whole topic here

at the end of this Website the user shows possible things one could do like so:

Now that we're logged in, we could: Set the $currentDocument variable to $ieObject.Document as the value is now different since a new page has been loaded. Parse out information we need by exploring the webpage. Automate looking for a specific post

But for some reason I cant produce this step. I get the same Error every time: You cannot call a method on a null-valued expression

My guesses, it cant reach the ID tag since the ComObject is still at the old Login Website since I didn't navigate to the new Link it provides after logging into the Site.

Any solutions/ideas?

Update 1.)

You can show with ComObjectName.Windows() the status of you're ComObject. I saw that both the LocationURL and the LocationName points to the (login successful session) subsite after login. Meaning the code of refreshing you're ComObject with the new Site worked.

example: $currentdocument = $woop.document


Solution

  • That's the point. You can't reach the element directly in an iframe. You need to get the iframe first, then use contentWindow.document to get the elements in the iframe.

    For example, you can use the code below to reach an element in an iframe:

    $ie.document.getElementById('iframeId').contentWindow.document.getElementById('elementId').
    

    You can change the code according to your actual situation.

    Reference link:

    (1) HTMLIFrameElement.contentWindow

    (2) Accessing a document's IFRAME via Powershell