【问题标题】:Powershell Script to click on an onclick img alt HTML element on websitePowershell脚本单击网站上的onclick img alt HTML元素
【发布时间】:2016-04-22 15:42:12
【问题描述】:

我正在尝试让我的 powershell 脚本单击网站上的以下对象。

源代码:

<a onclick="document.selectAccountsGroupsForm.action='manageAccountsGroups.do?method=showAllAccounts';document.selectAccountsGroupsForm.submit();">
   <img alt="showAllAccounts" src="/TNE/images/common/buttons/btn_showAllAccounts.gif">
</a>
<input name="accountSelectionCBChecked" type="hidden" value="false">

Powershell 代码

$ie = new-object -Com InternetExplorer.Application;
$ie.visible = $true;
$ie.navigate($url);
while($ie.busy) {Start-Sleep -Milliseconds 1000;}

$ie.Document.getElementById(“userName”).value = $Username 
$ie.Document.getElementByID(“password”).value = $Password 
$ie.Document.getElementById(“loginBtn”).Click()
$link=$ie.Document.getElementsByTagName('A') | Where-Object {$_.href -eq "document.selectAccountsGroupsForm.action='manageAccountsGroups.do?method=showAllAccountGroups';document.selectAccountsGroupsForm.submit();"}
$link.click()

注意:用户名、密码和 URL 变量未发布

错误

You cannot call a method on a null-valued expression. 
At H:\Desktop\Scripts\TEST.ps1:56 char:1 
+ $link.click() 
+ ~~~~~~~~~~~~~ 
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException 
    + FullyQualifiedErrorId : InvokeMethodOnNull –

【问题讨论】:

  • 您发布的代码有什么问题? IE是唯一可以使用的浏览器吗?
  • 是的,我只能使用 IE,因为我正在使用这个脚本工作。我遇到的问题是我需要脚本单击一个名为“显示所有帐户”的按钮,单击该按钮时会按照它说的那样进行
  • 你发布的代码怎么不起作用?
  • 当我在 Windows Powershell 上运行代码时,我收到以下错误:您不能在空值表达式上调用方法。在 H:\Desktop\Scripts\TEST.ps1:56 char:1 + $link.click() + ~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
  • 您应该在原始帖子中发布异常等详细信息,而不是几天后在评论中发布。

标签: java html css powershell


【解决方案1】:

正如错误中解释的那样,您的问题来自以下行不返回任何值的事实:

$link=$ie.Document.getElementsByTagName('A') | Where-Object {$_.href -eq "document.selectAccountsGroupsForm.action='manageAccountsGroups.do?method=showAllAccountGroups';document.selectAccountsGroupsForm.submit();"}

编辑

你也许可以测试一下:

$link=$ie.Document.getElementsByTagName('A') | Where-Object {$_.outerhtml -like "*document.selectAccountsGroupsForm.action*"}

【讨论】:

  • 如何编辑代码使其返回值?
  • 我测试了上面的代码,PowerShell返回了上面描述中列出的相同错误
【解决方案2】:

当我开发 Powershell 代码来控制 IE 时,我做了几件事来调试脚本。你可以做一个或两个。我建议先尝试第一个:

  1. 使用 Powershell 调试器。尝试在引用$ie.Document 的第一行设置断点(Set-BreakPoint)。然后逐行单步执行(? 寻求帮助,v 单步执行)并检查脚本每一行的结果值。例如,在您分配给$ie.Document.getElementById(“userName”).value 后,在调试器中键入该表达式并查看它是否产生您期望的用户名。当您到达getElementsByTagName 的行时,您可能希望在调试器中执行类似$test = $ie.Document.getElementsByTagName('A') 的内容,然后查看$test 中的内容。在您准确了解 $test 中的内容后,您可以调整 where-object 语句以获得您想要的内容。或者,如果该元素甚至不在 $test 中,您就会知道。
  2. 编写一些调试函数,例如检查getElementsByTagName 的结果。在调试器中,您可以将这些调试函数作为速记执行(而不是手动输入较长的表达式)。除非您将使用 PS 和 IE 进行大量开发/调试,否则这可能没有意义。

【讨论】:

    【解决方案3】:

    您正在尝试通过“href”过滤元素,但您的来源没有它。

    【讨论】:

      猜你喜欢
      • 2021-01-07
      • 1970-01-01
      • 2017-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多