Get-BrowserControl.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function Get-BrowserControl
{
    <#
    .Synopsis
        Gets a control from a browser
    .Description
        Gets a control from a browser. Controls can be selected by id, name, tagname, and innertext
    .Link
        Get-BrowserControl
    .Link
        Open-Browser
 
    #>

    [CmdletBinding(DefaultParameterSetName='Id')]
    [OutputType([PSObject], [string])]

    param(
     # The Browser Object.
    [Parameter(Mandatory=$true,
        ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true)]
    [ValidateScript({
        if ($_.psobject.typenames -notcontains 'System.__ComObject' -and -not $_.Quit) {
            throw "Not IE"
        }
        $true
    })]
    $IE,
    # The ID of the object within the page
    [Parameter(Mandatory=$true, ParameterSetName='ById')]
    [string]$Id,
    # The name of the object within the page
    [Parameter(Mandatory=$true, ParameterSetName='ByName')]
    [string]$Name,
    # The tag name of the object within the page
    [Parameter(Mandatory=$true, ParameterSetName='ByTagName')]
    [string]$TagName,
    
    
    # Will find a tag title within items a specific tag
    [string]$TagTitle,
    
    # Will find a css class within items of a specific tag or name
    [string]$CssClass,
    
    # Will find a link that points to a particular HREF
    [Parameter(Mandatory=$true, ParameterSetName='ByHref')]
    [string]$Href,
    
    # The property of the document object
    [Parameter(Mandatory=$true, ParameterSetName='ByInnerText')]
    [string]$DocumentProperty,
    # The inner text to find.
    [Parameter(Mandatory=$true, ParameterSetName='ByInnerText')]
    [string]$InnerText,
    # If set, will find elements that have an innertext like the value, rather than an exact match
    [Parameter(ParameterSetName='ByInnerText')]
    [switch]$Like
    )
    
    process {
        #region Get the Controls
        if ($psCmdlet.ParameterSetName -eq 'ById') {
            $ie.Document.getElementById($id)     
        } elseif ($psCmdlet.ParameterSetName -eq 'ByName') {
            $ie.Document.getElementById($name)      
        } elseif ($psCmdlet.ParameterSetName -eq 'ByHref') {
            $ie.Document.getElementsByTagName("A") |
                Where-Object { $_.Href -eq $HRef}             
        } elseif ($psCmdlet.ParameterSetName -eq 'ByTagName') {
            $found = $ie.Document.getElementsByTagName($tagname)
            if ($tagTitle) {
                $Found = $found | 
                    Where-Object { $_.Title -eq $tagTitle } 
            }
            if ($CssClass) {
                $found = $found 
                
            }
        } elseif ($psCmdlet.ParameterSetName -eq 'ByInnerText') {
            foreach ($obj in $ie.Document.$DocumentProperty) {
                if ($like) {
                    if ($obj.InnerText -like "$InnerText") {
                        $obj
                    }
                } else {
                    if ($obj.InnerText -eq "$InnerText") {
                        $obj
                    }
                }
            }
        }
        #endregion Get the Controls
    }
}