简体   繁体   English

Powershell:无法对空值表达式调用方法

[英]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.大家,我刚开始学习 powershell 现在我刚开始练习一些脚本,我想我的第一个脚本应该是这样的,它让我打开浏览器,点击登录,填写信息,让我登录,并在我的工作场所签到。

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.我在编码和 powershell 方面都很糟糕,因为我对此很陌生(这基本上是我的第二天)所以如果我能得到简化版本,那就太好了。

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?我现在的问题是,在这种情况下,我如何为 function $ 链接添加值,或者我如何让它点击底部的那个按钮?

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.例如,您显示的 URL 有很多类和 Div、框架。 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.请注意,返回的数组中有多个“A”,它们是从零开始的,但在这种情况下你只想要第二个,但它实际上位于索引 1,而不是 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.然而,请注意“Log ind”的文本是如何呈现的。

(($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.因此,不包含空格的“-eq”将无法找到它。

As noted referencing the correct object grants access to the click method.如前所述,引用正确的 object 授予对 click 方法的访问权限。 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.再次,花必要的时间通过我在原始评论中提到的指针加快 PowerShell 的速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Powershell:无法在出现空值表达式错误时调用方法 - Powershell: cannot call a method on a null-valued expression error powershell &#39;您不能在空值表达式上调用方法&#39; - powershell 'You cannot call a method on a null-valued expression' Powershell:您不能在空值表达式上调用方法 - Powershell: You cannot call a method on a null-valued expression PowerShell-如果则:无法在空值表达式上调用方法 - PowerShell - If Then: cannot call a method on a null-valued expression Powershell:您不能在空值表达式上调用方法 - Powershell : You cannot call a method on a null-valued expression 您不能使用 excel 在 powershell 中调用空值表达式的方法 - You cannot call a method on a null-valued expression in powershell with excel 您不能对空值表达式调用方法。 Powershell - You cannot call a method on a null-valued expression. Powershell 无法发送Powershell电子邮件-您无法在空值表达式上调用方法 - Cannot send powershell email - You cannot call a method on a null-valued expression 入门无法在Powershell词典中的空值表达式上调用方法 - Getting You cannot call a method on a null-valued expression in Powershell dictionary 您不能在空值表达式上调用方法。 通过 powershell 在网页中单击按钮 - You cannot call a method on a null-valued expression. clicking button in webpage via powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM