Link to home
Start Free TrialLog in
Avatar of Leo Torres
Leo TorresFlag for United States of America

asked on

Powershell and HTML with IE

Code Below works fine now.
But I need to make a change to the way IE is called. I want IE to close after it test a link and open a whole new instance of IE to do the next test. Now I pass links in a foreach loop and all the links use the same window. I need to open close IE when each link is passed to function below. How would that change the code below.

<#	#>
if (!(Get-Variable ie -Scope global -ea SilentlyContinue) -or !$ie.Document)
{
	$global:ie = New-Object -comobject InternetExplorer.Application
}
$ie.visible = $true
$ie.silent = $true

$global:ReportPart = "/interface/folderTree.jsp?rootId=216&expandLevel=1&clearUIPath=true&uiPathLabel=Reports"
#                     /interface/folderTree.jsp?rootId=216&amp;expandLevel=1&amp;clearUIPath=true&amp;uiPathLabel=My+Reports    # sample Akebono Brake Corporation	UTA 6.1

function Test-ReportSite ([String] $url, [String] $login, [String] $password)
{
	$ie.Navigate2($url)
	while ($ie.busy) { Start-Sleep -m 100 }
	while ($ie.Document.readyState -ne 'Complete') { Start-Sleep -m 100 }
	
	$ie.Document.getElementsByTagName("input") | ? { $_.Id -eq 'loginField' } | % { $_.value = $login }
	$ie.Document.getElementsByTagName("input") | ? { $_.Id -eq 'passwordField' } | % { $_.value = $password }
	$ie.Document.getElementsByTagName("input") | ? { $_.name -eq 'password' -and $_.type -eq "password" } | % { $_.value = $password }

	
	$ie.Document.getElementsByTagName("button") | ? { $_.Type -eq 'button' } | % { $_.Click() }
	while ($ie.Document.readyState -ne 'Complete') { Start-Sleep -m 100 }
	
	$ie.Navigate2($url + $ReportPart)
	while ($ie.Document.readyState -ne 'Complete') { Start-Sleep -m 100 }
	
	$link = $ie.Document.getElementsByTagName("a") | ? { $_.InnerText -like '*Overtime Report' } | select -First 1 -expandProperty href
	if (!$link)
	{
		$link = $ie.Document.getElementsByTagName("a") | ? { $_.InnerText -like '*Count' } | select -First 1 -expandProperty href
	}
	$ie.Navigate2($link)
	while ($ie.Document.readyState -ne 'Complete') { Start-Sleep -m 100 }
	
	$ie.Document.getElementsByTagName("button") | ? { $_.innerText -eq 'Go' } | % { $_.Click() }
	Start-Sleep 5
	
<#  Error message evaluation ignored ATM   
  $ie.Document.getElementsByTagName("span") | ft -a classname, tagname, innerText #Gets you error
#>
	
	$RunByTest = $ie.Document.body.tagname -eq 'FRAMESET' -and $ie.Document.body.id -eq 'reportViewerFrame'
	
	if ($RunByTest)
	{
		$TestValue = 200
	}
	else
	{
		$TestValue = -200
	}
	Write-Host "$URL`: $TestValue"
	return $TestValue
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Leo Torres

ASKER

Sorry been away from office have not been able to test. I think tomorrow I will be able to finally test.

Thanks Qlemo.
Works
There is probably a better way than to close IE, but I cannot see any means to reset it.
No Worries Thanks it works for me.

Results oriented person. :)