2016-02-23 144 views
0

我目前正在使用PowerShell腳本啓動頁面並單擊按鈕。該按鈕是一個JavaScript按鈕,沒有< < a >>標籤。兩種變體均返回您無法在空值表達式上調用方法。我確信我正在盤旋答案,但我仍然處於持有模式。提前致謝!單擊JavaScript按鈕的PowerShell腳本

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

回答

0

你的凡 - Object子句失敗,因爲innerText中有額外的空白,導致相等條件失敗。因此$link變量爲空。這會導致You cannot call a method on a null-valued expression錯誤。嘗試修剪空白。

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

感謝您的回覆。不幸的是即使使用修剪功能它仍然返回一個空值。我想知道如果$ ie.Document.getElementsByTagName('按鈕')是造成這個問題。 –

+0

奇怪的是,我有你的代碼在本地工作。 – Cobster

+0

看起來問題與Windows 10和PS 5有關。我發現有幾個線程有類似的問題。我從來沒有考慮過它,因爲錯誤並沒有真正指向那個方向。噓! –