In this part we will look at how we can handle multiple tabs in Internet Explorer and manipulate elements on them.

When using the Internet Explorer COM object, we can only get the document information of the latest tab we created. But sometimes we want to manipulate something on a tab we previously created. Unfortunately, the Internet Explorer COM object doesn’t have tabs functionality, so we will have to find another way.

First, let’s create a simple script that will open 2 tabs to different search engines. This is basically the code from part 1, slightly modified.

Setting up our base script.

$URLs = @(
    "https://www.yahoo.com",
    "https://www.bing.com"
)

$objInternetExplorer = New-Object -ComObject InternetExplorer.Application;
$objInternetExplorer.visible = $true;

#Set a bool for the first tab.
$bFirstTab = $true;
foreach($URL in $URLs)
{
    #If this is our first tab;
    if($bFirstTab)
    {
        #Open it
        $objInternetExplorer.navigate($URL);
        #Set the boolean to false.
        $bFirstTab = $false;
    }
    #If it is not our first tab.
    else
    {
        #Open it with in a new tab.
        $objInternetExplorer.navigate($URL, 0x0800);
    }
}

You should see your Internet Explorer opening 2 tabs to the search engines.

The object will only hold the information for the first page you opened. We can still easily manipulate the information on Yahoo (cause that’s the first tab we opened).

$htmlelementSearchField = $objInternetExplorer.Document.IHTMLDocument3_getElementById("uh-search-box");
$htmlelementSearchField.value = "Hello there, yahoo!"

But we have no way to access the information on the Bing tab from within our current Internet Explorer object.

Getting all Internet Explorer ‘windows’.

To get around this issue, we need to enumerate all current open Internet Explorer windows. We can use another COM object to achieve this.

 $objApplicationShell = New-Object -ComObject Shell.Application 

You can do a lot from this object, however the thing we are interested in is the .Windows() method.

$objApplicationShell.Windows();

If your run this, it will dump all it’s information on any open window, including our Internet Explorer windows / tabs. We can filter these out by using the Fullname property. Any Internet Explorer windows will have a FullName like “C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE”.

Using the following code should dump our 2 open internet explorer tab objects:

$objInternetExplorerWindows = $objApplicationShell.Windows() | Where-Object {($_.FullName -ilike "*iexplore.exe")}
$objInternetExplorerWindows | Format-Table;

We can loop over them to find out which on is which:

foreach($objInternetExplorerWindow in $objInternetExplorerWindows)
{
    Write-Host $objInternetExplorerWindow.LocationURL;
}

If we want to manipulate the search fields for each search engine, we can do the following:

#Loop through our Internet Explorer windows (/tabs)
foreach($objInternetExplorerWindow in $objInternetExplorerWindows)
{
    #If the site contains yahoo.com in the LocationURL (URL)
    if($objInternetExplorerWindow.LocationURL -like "*yahoo.com*")
    {
        #Enter some text in the yahoo search field.
        $htmlelementYahooSearchField = $objInternetExplorerWindow.Document.IHTMLDocument3_getElementById("uh-search-box");
        $htmlelementYahooSearchField.value = "Hello there, YAHOO!";
    }
    #If the site contains bing.com in the LocationURL (URL)
    if($objInternetExplorerWindows.LocationURL -like "*bing.com*")
    {
        $htmlelementBingSearchField = $objInternetExplorerWindow.Document.IHTMLDocument3_getElementById("sb_form_q");
        $htmlelementBingSearchField.value ="Hello there, BING!";
    }
}

And this will give us:


This is, of course, a very crude way of doing things. So in part 5 we will be looking at how we can create some functions to manage this a lot better.

Full Code

$URLs = @(
    "https://www.yahoo.com",
    "https://www.bing.com"
)

$objInternetExplorer = New-Object -ComObject InternetExplorer.Application;
$objInternetExplorer.visible = $true;

#Set a bool for the first tab.
$bFirstTab = $true;
foreach($URL in $URLs)
{
    #If this is our first tab;
    if($bFirstTab)
    {
        #Open it
        $objInternetExplorer.navigate($URL);
        #Set the boolean to false.
        $bFirstTab = $false;
    }
    #If it is not our first tab.
    else
    {
        #Open it with in a new tab.
        $objInternetExplorer.navigate($URL, 0x0800);
    }

    while($objInternetExplorer.ReadyState -ne 4) 
    {
        Start-Sleep -m 100;
    };
}


$objApplicationShell = New-Object -ComObject Shell.Application
$objInternetExplorerWindows = $objApplicationShell.Windows() | Where-Object {($_.FullName -ilike "*iexplore.exe")}
$objInternetExplorerWindows | Format-Table;

#Loop through our Internet Explorer windows (/tabs)
foreach($objInternetExplorerWindow in $objInternetExplorerWindows)
{
    #If the site contains yahoo.com in the LocationURL (URL)
    if($objInternetExplorerWindow.LocationURL -like "*yahoo.com*")
    {
        #Enter some text in the yahoo search field.
        $htmlelementYahooSearchField = $objInternetExplorerWindow.Document.IHTMLDocument3_getElementById("uh-search-box");
        $htmlelementYahooSearchField.value = "Hello there, YAHOO!";
    }
    #If the site contains bing.com in the LocationURL (URL)
    if($objInternetExplorerWindows.LocationURL -like "*bing.com*")
    {
        $htmlelementBingSearchField = $objInternetExplorerWindow.Document.IHTMLDocument3_getElementById("sb_form_q");
        $htmlelementBingSearchField.value ="Hello there, BING!";
    }
}

One Reply to “PowerShell // Internet Explorer Automation – Part 4 : Handling multiple tabs”

Leave a Reply

Your email address will not be published.