【问题标题】:powershell IE automation href issuepowershell IE自动化href问题
【发布时间】:2017-10-02 01:35:10
【问题描述】:

我正在尝试使用 powershell 访问 Jtrac 网页。我可以登录,但我无法访问搜索按钮哪个 href 链接。

$Url = “http://kbserver/workflow/app/login”
enter code here`$Username=”XXXXX”
enter code here`$Password=”XXXXX”
$IE = New-Object -com internetexplorer.application;
$IE.visible = $true;
$IE.navigate($Url);
while ($IE.Busy -eq $true) 
{ 
Start-Sleep -Milliseconds 2000; 
} 
$Login = $IE.document.getElementById("loginName3").value = "$Username" 
$Login = $IE.Document.getElementById(“password12”).value= "$Password" 
$Login = $IE.Document.getElementsByTagName("input") | where-object   {$_.type -eq "submit"}
$Login.click();
while ($IE.Busy -eq $true) 
{ 
Start-Sleep -Milliseconds 5000; 
} 
$Login = $IE.Document.getElementsByTagName("a") | where {$_.href -eq "'?wicket:interface=:2:table:dashboardRows:3:dashboardRow:search::ILinkListen     er::'"}
$Login.click();

我得到的错误是

您不能在空值表达式上调用方法。 在 C:\Users\Dinesh\Webbb.ps1:20 char:13 + $Login.click

<a href="?wicket:interface=:2:table:dashboardRows:3:dashboardRow:search::ILinkListener::">
<img title="SEARCH" src="../resources/search.gif"> </a>

【问题讨论】:

    标签: html powershell automation


    【解决方案1】:

    您在第 19 行中似乎有一个错字,ILinkListen er:: 在您的 where 末尾,而不是 ILinkListener::。 where 没有找到任何匹配的东西,所以它返回 null,这就是你获取 null 值 $Login 对象的方式。

    编辑:您的问题仍然是第 19 行,您应该打印出 $IE.Document.getElementsByTagName("a") 并验证它实际上返回了您期望的内容,因为您的 where 过滤器不匹配任何内容并将 $null 返回到 $Login。

    Edit2:去了一个网站并抓取了所有的href并看了一下,powershell从href中删除了“”。

    你应该改用$IE.Document.getElementsByTagName("a") | where {$_.href -eq "?wicket:interface=:2:table:dashboardRows:3:dashboardRow:search::ILinkListener::"}

    编辑 3:您的第二个问题可以通过使用 -match 运算符而不是 -eq 的正则表达式来解决(如果您正在检查的项目是动态的,则 -eq 不会真正起作用)。

    像这样:

    $IE.Document.getElementsByTagName("a") | where {$_.href -match "\?wicket:interface=:\d"}

    此正则表达式将返回任何包含“?wicket:interface=:digit”的内容。 一位数是这里的最佳选择,因为它会在刷新时不断增长,假设页面上没有其他内容与这应该是好的匹配。

    【讨论】:

    • 你能发布 $IE.Document.getElementsByTagName("a") 的输出吗?
    • Write-Host $IE.Document.getElementsByTagName("a") mshtml.HTMLAnchorElementClass mshtml.HTMLAnchorElementClass mshtml.HTMLAnchorElementClass mshtml.HTMLAnchorElementClass 这样我没有更多的行...
    • Write-Host 只会给你类名,只要这样称呼它: $IE.Document.getElementsByTagName("a") |选择href
    • 这就是我得到的kbserver/workflow/app/…::
    • $Login = $IE.Document.getElementsByTagName("a") | where-object {$_.href -eq "kbserver/workflow/app/…::"} $Login.Click() 所以我试过这样仍然是同样的错误 PS C:\Users\Dinesh> .\Webbb.ps1 你不能调用空值表达式上的方法。在 C:\Users\Dinesh\Webbb.ps1:20 char:13 + $Login.Click
    猜你喜欢
    • 2016-09-30
    • 1970-01-01
    • 2021-11-08
    • 2018-08-31
    • 1970-01-01
    • 2010-10-02
    • 2010-12-06
    • 2012-09-28
    • 2012-06-15
    相关资源
    最近更新 更多