【问题标题】:PowerShell throws error saying Click() does not existPowerShell 抛出错误说 Click() 不存在
【发布时间】:2019-06-21 03:05:39
【问题描述】:

我正在练习使用 PowerShell,今天我想我会编写一个脚本来将我登录到一个网站。我对这个具体的例子没有用,但我认为这是一个很好的做法。我在引用this 时创建了它。这是我目前所拥有的。

$ie = New-Object -ComObject "InternetExplorer.Application"
$ie.Visible = $true

$username = "my@email"
$password = "password"

$ie.Navigate("https://squirrel.ws/login")

While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}

$usernameField = $ie.Document.getElementByID('UserEmail')
$usernameField.value = $username

$passwordField = $ie.Document.getElementByID('UserPassword')
$passwordField.value = $password

$link = $ie.Document.getElementByID('$0')
$link.click()

$ie.Quit()

当我运行它时,会出现一个 IE 窗口,并且几乎立即关闭并显示以下错误:

Method invocation failed because [System.DBNull] does not contain a method named 'click'.
At J:\removingForPrivacy\test.ps1 char:1
+ $link.click()
+ ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

所以我很确定它在尝试单击时会引发错误,或者因为我错误地标记了按钮,或者两者兼而有之。在尝试创建它时,我找不到按钮的 ID,所以我使用了我能找到的东西。否则我不确定为什么click() 在我看到的所有示例中都不存在。有人可以帮帮我吗?

【问题讨论】:

    标签: powershell internet-explorer click


    【解决方案1】:

    我的问题来自于在创建$Link 时,我使用了错误的标签。我使用了所有相同的代码,但将 $Link 创建行换成了:

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

    【讨论】:

      猜你喜欢
      • 2015-01-08
      • 1970-01-01
      • 2016-10-24
      • 2016-09-13
      • 2019-02-13
      • 2017-11-23
      • 2021-04-16
      • 2014-10-13
      • 1970-01-01
      相关资源
      最近更新 更多