简体   繁体   English

<BUTTON>在 HTML 页面中</button>单击

[英]Click on <BUTTON> in HTML page

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

I am trying to click/submit the above button using Windows Powershell.我正在尝试使用 Windows Powershell 单击/提交上述按钮。 I have tried the following code:我尝试了以下代码:

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

which brings back this error:这带来了这个错误:

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

I also tried this code:我也试过这个代码:

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

but this also brings back the same error as before.但这也带来了与以前相同的错误。

my powershell code in full:我的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() 

EDIT编辑

Here is the output of powershell getelementbytagname.这是 powershell getelementbytagname 的输出。 Notice there is no classname or ID, how does it get clicked?注意没有类名或 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

EDIT编辑

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

In the code you shared the $doc variable is not getting initialized.在您共享的代码中, $doc变量没有被初始化。 Please initialize it first before using it in the below line:在以下行中使用之前,请先对其进行初始化:

$doc.documentElement.getElementsByClassName('button')

The error on this line, ie null-valued expression error, is due to the fact that $doc is null at this point.这一行的错误,即null-valued expression错误,是由于此时$doc为空。

Alternate Solution: Instead try the below code to get the submit button in the form on the page.替代解决方案:而是尝试以下代码以获取页面上表单中的提交按钮。 You can tweak the code as per your requirement and the page you are targeting.您可以根据自己的要求和目标页面调整代码。

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

the closest I could get to this type of automation was by writing this:我最接近这种类型的自动化是通过写这个:

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

this code clicked all the buttons on the page but did get me logged into the webpage.这段代码点击了页面上的所有按钮,但确实让我登录了网页。

on further reading( http://www.w3schools.com/jsref/met_namednodemap_item.asp ) I have been tweaking the code using:进一步阅读( http://www.w3schools.com/jsref/met_namednodemap_item.asp )我一直在使用以下方法调整代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM