用于单击JavaScript按钮的PowerShell脚本

时间:2016-02-23 15:55:09

标签: javascript powershell

我目前正在使用PowerShell脚本来启动页面并单击按钮。该按钮是一个JavaScript按钮,没有<< a>>标签。两种变体都返回您无法在空值表达式上调用方法。我确定我在回答这个问题,但我仍处于控股状态。提前谢谢!

    $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>

1 个答案:

答案 0 :(得分:0)

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

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