PowerShell 脚本,用于单击 JavaScript 按钮

PowerShell script to click a JavaScript Button

本文关键字:JavaScript 按钮 单击 用于 脚本 PowerShell      更新时间:2023-09-26

我目前正在处理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>

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

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