簡體   English   中英

<BUTTON>在 HTML 頁面中</button>單擊

[英]Click on <BUTTON> in HTML page

<button type="submit">Login</button>

我正在嘗試使用 Windows Powershell 單擊/提交上述按鈕。 我嘗試了以下代碼:

$submitButton = $doc.documentElement.getElementsByClassName('button') |
                Select-Object -First 1
$submitButton.click()

這帶來了這個錯誤:

You cannot call a method on a null-valued expression.
ps1:15 char:1
+     $submitButton = $doc.documentElement.getElementsByClassName('button') | ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
ps1:16 char:1
+ $submitButton.click()
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

我也試過這個代碼:

$loginBtn = $ie.Document.getElementsById('input') |
            Where-Object {$_.Type -eq 'button' -and $_.Value -eq 'LoginButton'}
$loginBtn.click()

但這也帶來了與以前相同的錯誤。

我的PowerShell代碼完整:

$username='USERNAME' 
$password='PASSWORD'

$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true
$ie.Navigate("URL EXAMPLE")

while ($ie.Busy -eq $true){Start-Sleep -seconds 1;}   

$Open = $ie.Document.getElementByID('has-account')
$Open.click()

$usernamefield = $ie.Document.getElementByID('login-usr').value =          $username
$passwordfield = $ie.Document.getElementByID('login-pass').value =     $password
$submitButton = $doc.documentElement.getElementsByClassName('button') |     Select-Object -First 2
$submitButton.click() 

編輯

這是 powershell getelementbytagname 的輸出。 注意沒有類名或 ID,它是如何被點擊的?

className                    : 
id                           : 
tagName                      : BUTTON
parentElement                : System.__ComObject
style                        : System.__ComObject
document                     : mshtml.HTMLDocumentClass
sourceIndex                  : 60
offsetLeft                   : 61
offsetTop                    : 220
offsetWidth                  : 320
offsetHeight                 : 41
offsetParent                 : System.__ComObject
innerHTML                    : Login
innerText                    : Login
outerHTML                    : <button type="submit">Login</button>
outerText                    : Login
parentTextEdit               : System.__ComObject

編輯

<form id="sp-login-form" action="#" method="post">
        <button type="button" id="fb-login-btn" class="button fb">Log in with Facebook<span style="position: absolute;"></span></button>
        <em>or</em>
        <div>
          <label for="login-usr">Username</label>
          <input type="text" name="username" id="login-usr" placeholder="Spotify username">
          <label for="login-pass" class="pass">Password</label>
          <input type="password" name="password" id="login-pass" class="pass" placeholder="Password">
          <button type="submit">Login</button>
        </div>
      </form>

在您共享的代碼中, $doc變量沒有被初始化。 在以下行中使用之前,請先對其進行初始化:

$doc.documentElement.getElementsByClassName('button')

這一行的錯誤,即null-valued expression錯誤,是由於此時$doc為空。

替代解決方案:而是嘗試以下代碼以獲取頁面上表單中的提交按鈕。 您可以根據自己的要求和目標頁面調整代碼。

$submitButton=$ie.Document.getElementsByTagName("button") | Where-Object {$_.innerhtml -eq 'Login'}
$submitButton.click();

我最接近這種類型的自動化是通過寫這個:

$Click=$ie.Document.getElementsByTagName("button") | Where-Object {$_.type -eq 'submit'}
$Click.click();

這段代碼點擊了頁面上的所有按鈕,但確實讓我登錄了網頁。

進一步閱讀( http://www.w3schools.com/jsref/met_namednodemap_item.asp )我一直在使用以下方法調整代碼:

$Link = $ie.Document.getElementsByTagName("button")| where-object {$_.type -eq "submit"} $Link.item(2).click()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM