方法" getElementsByClassName"未识别

时间:2016-01-14 08:44:13

标签: windows internet-explorer powershell

我想启动Windows资源管理器并登录网站。登录后我想点击退出textlink。但是我收到了这个错误:

Method invocation failed because [mshtml.HTMLDocumentClass] doesn't contain a
method named 'getElementsByClassName'
At C:\Users\ntando.ntuli\Desktop\test.ps1:29 char:43
+  $Link=$ie.Document.getElementsByClassName <<<< ("underline") | Where-Object {$_.ClassName -eq "underline"}
    + CategoryInfo          : InvalidOperation: (getElementsByClassName:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

以下是我正在使用的代码

$IE = New-Object -COM InternetExplorer.Application;
$IE.Visible = $true;
$IE.Navigate("http://192.168.2.73:6500/ouaf/loginPage.jsp");

# Wait a few seconds and then launch the executable.
while ($IE.Busy -eq $true) {
  Start-Sleep -Milliseconds 2000;
}

# The following UsernameElement, PasswordElement, and LoginElement need to be
# modified first.  See the notes at the top of the script for more details.

$elementMatchText = "You are logged in as English System"
$IE.Document.getElementById("userId").value = "username"
$IE.Document.getElementByID("password").value="password"
$IE.Document.getElementById("loginButton").Click()

while ($IE.Busy -eq $true) {
  Start-Sleep -Milliseconds 2000; 
} 

#Logout textlink classname
$Link = $ie.Document.getElementsByClassName("underline") |
        Where-Object {$_.ClassName -eq "underline"}
$Link.Click()

2 个答案:

答案 0 :(得分:3)

您似乎只能在文档的getElementsByClassName属性上调用documentElement

$ie.Document.documentElement.getElementsByClassName("underline")

答案 1 :(得分:0)

根据documentation,即使工作,我之前看到getElementsByClassName()失败了。解决方法是按标记名称选择元素(您似乎在寻找<a>标记),然后过滤ClassName属性。如果找到多个匹配元素,我还会将结果限制为第一次出现:

$Link = $ie.Document.getElementsByTagName('a') |
        Where-Object { $_.ClassName -eq 'underline' } |
        Select-Object -First 1