【问题标题】:PowerShell script to click a JavaScript Button用于单击 JavaScript 按钮的 PowerShell 脚本
【发布时间】:2016-02-24 02:47:51
【问题描述】:

我目前正在使用 PowerShell 脚本来启动页面并单击按钮。该按钮是一个没有 > 标记的 JavaScript 按钮。两种变体都返回您不能在空值表达式上调用方法。我确定我在盘旋答案,但我仍然处于等待状态。提前致谢!

    $ie = new-object -com internetexplorer.application
    $ie.visible=$true
    $ie.navigate('http://192.168.63.10/counters/usage.php')
    while($ie.busy) {sleep 1}

    $link = @($ie.Document.getElementsByTagName('button')) | Where-Object {$_.innerText -eq 'Download File to Your Computer'}
    $link.click()

我也试过了:

 $link2 =   $ie.Document.documentElement.getElementsByClassName('horizButtonBarAboveTableLeft')| Where-Object {$_.innerText -eq 'Download File to Your Computer'}
$link2.click()

这是 HTML(希望使用 GenerateCsvFile() 来单击按钮:)

<div class="horizButtonBarAboveTableLeft">
<button onclick="document.location=document.URL">
    <img src="/images/counters/refresh_renew_32.png" alt="" width="32" height="32">
    Refresh    </button>

<img src="/images/ClearGIF.gif" width="19" height="20" alt="">

<button onclick="GenerateCsvFile();">
    <img src="/images/counters/download_import_32.png" alt="" width="32" height="32">
    Download File to Your Computer    </button>

【问题讨论】:

    标签: javascript powershell


    【解决方案1】:

    您的 Where-Object 子句失败,因为 innerText 中有额外的空格导致相等条件失败。因此$link 变量为空。这会导致You cannot call a method on a null-valued expression 错误。尝试修剪空白。

    Where-Object { $_.innerText.Trim() -eq 'Download File to Your Computer' }
    

    【讨论】:

    • 感谢您的回复。不幸的是,即使使用 Trim 函数,它仍然返回 null。我想知道 $ie.Document.getElementsByTagName('button') 是否导致问题。
    • 看起来这个问题与 Windows 10 和 PS 5 有关。我发现了几个有类似问题的线程。我从来没有考虑过,因为错误并没有真正指向那个方向。嘘!
    • 我假设问题出在 InternetExplorer com 对象上。
    • 是的,这就是问题所在。从那以后,我使用运行良好的 Invoke-Webrequest 以另一种方式解决了该任务。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    相关资源
    最近更新 更多