简体   繁体   中英

Powershell: Cannot call a method on a null-valued expression

everyone, i am just starting out with learning powershell and right now i just started practicing with some scripts, and i thought my first script should be one, that lets me open a browser, click the login, fill in the information, log me in, and sign me in at my workplace.

This is the code i've written so far:

    $ie = New-Object -com InternetExplorer.Application #åbner internet explorer
    $ie.visible=$true #gør den synlig
    
    $ie.navigate('https://itd-skp.sde.dk/')
    
    while ($ie.Busy -eq $true){Start-Sleep -seconds 4;}
    
    $link = $ie.Document.getElementsByTagname('A') | Where-Object {$_.innerText -eq 'Log ind'}
    $link.click()

这是我希望它点击的网页

这是代码行,即引用需要单击的链接

This is all of the code of the website, which i have access to, so i hope this is enough.

I suck at both coding and powershell, since i am fairly new with this (this is basically my second day) so if i can get the dumbed down versions, that would be nice.

My question is now, how do i add value to the function $link, in this case, or how do i get it to click that button on the bottom?

Continuing from my comment...

Some sites just don't allow or inhibit automation and then there are sites that what you see is not what really is. You have to dig at it. For Example the URL you show, has a lot of classes and Divs, frames. So, if what you are after is embedded in those, then more code is required.

You also need to dig at how to scrape a site to see the objects that can be worked with.

A simple scrape shows this...

# Scrape the site for object info.
$url = 'https://itd-skp.sde.dk'
($WebSite = Invoke-WebRequest -Uri $url -SessionVariable fe) 
$WebSite.Links
# Results
<#
innerHTML : Registrering lukket
innerText : Registrering lukket
outerHTML : <a class="col-xs-12 btn btn-primary btn-load btn-lg disabled" role="button" style="cursor: not-allowed;" href="javascript:void(0);">Registrering 
            lukket</a>
outerText : Registrering lukket
tagName   : A
class     : col-xs-12 btn btn-primary btn-load btn-lg disabled
role      : button
style     : cursor: not-allowed;
href      : javascript:void(0);

innerHTML : 
                                        Log ind
innerText :  Log ind
outerHTML : <a class="col-xs-12 btn btn-primary btn-load btn-lg" href="admin/login.php">
                                        Log ind</a>
outerText :  Log ind
tagName   : A
class     : col-xs-12 btn btn-primary btn-load btn-lg
href      : admin/login.php
#>

Load and Use the info on the site

$IE = New-Object -ComObject 'InternetExplorer.Application'
$FormElementsequestURI = 'https://itd-skp.sde.dk'

$IE.Visible = $true
$IE.Silent  = $true
$IE.Navigate($FormElementsequestURI)
While ($IE.Busy) {Start-Sleep -Milliseconds 100}
$Doc  = $IE.Document
<#
$Doc.getElementsByTagName('a')

# Or simply 

$Doc.links | 
Select-Object -Property className, id, tagname, innertext, outertext, href, ie8_href
# Results
<#
className : col-xs-12 btn btn-primary btn-load btn-lg disabled
id        : 
tagName   : A
innerText : Registrering lukket
outerText : Registrering lukket
href      : javascript:void(0);
ie8_href  : javascript:void(0);

className : col-xs-12 btn btn-primary btn-load btn-lg
id        : 
tagName   : A
innerText :  Log ind
outerText :  Log ind
href      : https://itd-skp.sde.dk/admin/login.php
ie8_href  : https://itd-skp.sde.dk/admin/login.php
#>

Step through the elements to find which has a click method

$Doc.links | 
ForEach-Object {
[PSCustomObject]@{
    className   = $PSItem.className
    tagName     = $PSItem.tagName
    innerText   = $PSitem.innerText
    outerText   = $PSitem.outerText
    href        = $PSItem.href 
    ClickMethod = $PSItem.Click
}} | Format-Table -AutoSize
# Results
<#
className                                          tagName innerText           outerText           href                                   ClickMethod       
---------                                          ------- ---------           ---------           ----                                   -----------       
col-xs-12 btn btn-primary btn-load btn-lg disabled A       Registrering lukket Registrering lukket javascript:void(0);                    System.__ComObject
col-xs-12 btn btn-primary btn-load btn-lg          A        Log ind             Log ind            https://itd-skp.sde.dk/admin/login.php System.__ComObject
#>

Note there are multiple 'A' in the returned array, which are zero-based, but you only want the second on in this case, but that is really at index 1, not 2.

# Or

($Doc.getElementsByTagName('A') | 
Get-Member -MemberType Method) -match 'click'
# Results
<#
   TypeName: System.__ComObject#{3050f502-98b5-11cf-bb82-00aa00bdce0b}

Name  MemberType Definition   
----  ---------- ----------   
click Method     void click ()
#>

Yet, notice how the text for 'Log ind' renders.

(($Doc.getElementsByTagName('A')[1]).outerText).Length
# Results
<#
8
#>

(($Doc.getElementsByTagName('A')[1]).innerText).Length
# Results
<#
8
#>

It's got spaces. So, '-eq' without including the space will fail to find it.

As noted referencing the correct object grants access to the click method. Yet as shown they come back as below...

$Doc.links[1].click()
# Results
<#
System.__ComObject
#>

$Link = $Doc.getElementsByTagName('A') | 
Where-Object {$PSItem.href -eq 'https://itd-skp.sde.dk/admin/login.php'}
# Results
<#
System.__ComObject
#>

So,all-in-all one, for this site could just to this.

$IE = New-Object -ComObject 'InternetExplorer.Application'
$FormElementsequestURI = 'https://itd-skp.sde.dk'

$IE.Visible = $true
$IE.Silent  = $true
$IE.Navigate($FormElementsequestURI)
While ($IE.Busy) {Start-Sleep -Milliseconds 100}
$Doc  = $IE.Document
$Doc.links[1].click()

This just gets you pass your main login page (per you post query). Dealing with the new page is a new effort.

Again, spend the needed time getting up to speed on PowerShell via the pointers I call out in my original comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM