3

使用 powershell 进行 web ui 自动化。出现异常:invoke 方法失败,因为 [System.__ComObject] 不包含“click”方法。

可以点击提交类型输入吗?

我使用 getElementsByTagName getElementsByClassName getElementsByName ,不起作用。

任何人都可以帮助我吗?

powershell代码如下:

# open the specified web site and commit the key
$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("http://gitlab.alibaba-inc.com/keys/new")
$ie.visible = $true
while($ie.busy) {sleep 1}

$doc = $ie.document

# commit the button
$commit = $doc.getElementsByTagName("commit")

if($commit) 
{$commit.click()}

html源代码如下:

<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>
Profile | 
GitLab
</title>
<link href="/assets/favicon-4b751da746de7855d7eb8123072388ed.ico" rel="shortcut icon"    type="image/vnd.microsoft.icon" />
<link href="/assets/application-a9eac7f5b0c3b922de8997ae9ad74ab0.css" media="screen" rel="stylesheet" type="text/css" />
<script src="/assets/application-61398d184a36e6ae900134f123d5d649.js" type="text/javascript"></script>
<meta content="authenticity_token" name="csrf-param" />
<meta content="9SLFk6AwlsN2FoyO8xPY+M1hEbKfqlLTQ4CSDVc4efE=" name="csrf-token" />
<script type="text/javascript">
//<![CDATA[
window.gon =   {};gon.default_issues_tracker="gitlab";gon.api_version="v3";gon.api_token="xkMg31Ssva322SDF cgxY";gon.gravatar_url="http://www.gravatar.com/avatar/%{hash}?s=% {size}&d=mm";gon.relative_url_root="";
//]]>
</script>

</head>

<body class='ui_basic profile' data-page='keys:new'>
<header class='navbar navbar-static-top navbar-gitlab'>
<div class='navbar-inner'>
<div class='container'>
<div class='app_logo'>
<span class='separator'></span>
<a href="/" class="home has_bottom_tooltip" title="Dashboard"><h1>GITLAB</h1>
</a><span class='separator'></span>
</div>
<h1 class='project_name'>Profile</h1>
<ul class='nav'>
<li>
<a>
<div class='hide turbolink-spinner'>
<i class='icon-refresh icon-spin'></i>
Loading...
</div>
</a>
</li>
<li>
<div class='search'>
<form accept-charset="UTF-8" action="/search" class="navbar-form pull-left"  method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8"  type="hidden" value="&#x2713;" /></div>
<input class="search-input" id="search" name="search" placeholder="Search" type="text"   />
<input id="group_id" name="group_id" type="hidden" />
<input id="repository_ref" name="repository_ref" type="hidden" />

<div class='search-autocomplete-json hide' data-autocomplete-opts='[{"label":"project:  kelude2","url":"/kelude2"},{"label":"My Profile","url":"/profile"},{"label":"My SSH  Keys","url":"/keys"},{"label":"My Dashboard","url":"/"},{"label":"Admin  Section","url":"/admin"},{"label":"help: API Help","url":"/help/api"},{"label":"help:  Markdown Help","url":"/help/markdown"},{"label":"help: Permissions  Help","url":"/help/permissions"},{"label":"help: Public Access  Help","url":"/help/public_access"},{"label":"help: Rake Tasks  Help","url":"/help/raketasks"},{"label":"help: SSH Keys Help","url":"/help/ssh"}, {"label":"help: System Hooks Help","url":"/help/system_hooks"},{"label":"help: Web Hooks  Help","url":"/help/web_hooks"},{"label":"help: Workflow Help","url":"/help/workflow"}]'>   </div>
</form>

</div>

</li>
<li>
<a href="/public" class="has_bottom_tooltip" data-original-title="Public area"    title="Public area"><i class='icon-globe'></i>
</a></li>
<li>
<a href="/s/heyun" class="has_bottom_tooltip" data-original-title="Public area"    title="My snippets"><i class='icon-paste'></i>
</a></li>
<li>
<a href="/projects/new" class="has_bottom_tooltip" data-original-title="New project"    title="Create New Project"><i class='icon-plus'></i>
</a></li>
<li>
<a href="/profile" class="has_bottom_tooltip" data-original-title="Your profile"    title="My Profile"><i class='icon-user'></i>
</a></li>
<li>
<a href="/users/sign_out" class="has_bottom_tooltip" data-method="delete" data-original-title="Logout" rel="nofollow" title="Logout"><i class='icon-signout'></i>  
</a></li>
<li>
<a href="/u/heyun" class="profile-pic"><img alt="F3ea5164088694b48e4980e52d831927? s=26&amp;d=mm" src="http://www.gravatar.com/avatar/f3ea5164088694b48e4980e52d831927? s=26&amp;d=mm" />
</a></li>
</ul>
</div>
</div>
</header>

<div class='flash-container'>
</div>

<nav class='main-nav'>
<div class='container'><ul>
<li class="home"><a href="/profile" title="Profile"><i class='icon-home'></i>
</a></li><li class=""><a href="/profile/account">Account</a>
</li><li class=""><a href="/profile/notifications">Notifications</a>
</li><li class="active"><a href="/keys">SSH Keys
<span class='count'>1</span>
</a></li><li class=""><a href="/profile/design">Design</a>
</li><li class=""><a href="/profile/history">History</a>
</li></ul>
</div>
</nav>
<div class='container'>
<div class='content'><h3 class='page_title'>Add an SSH Key</h3>
<hr>
<div>
<form accept-charset="UTF-8" action="/keys" class="new_key" id="new_key" method="post">   <div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden"   value="&#x2713;" /><input name="authenticity_token" type="hidden"   value="9SLFk6AwlsN2FoyO8xPY+M1hEbKfqlLTQ4CSDVc4efE=" /></div><div class='clearfix'>
<label for="key_title">Title</label>
<div class='input'><input id="key_title" name="key[title]" size="30" type="text" />   </div>
</div>
<div class='clearfix'>
<label for="key_key">Key</label>
<div class='input'>
<textarea class="xxlarge thin_area" cols="40" id="key_key" name="key[key]" rows="20">
</textarea>
<p class='hint'>
Paste your public key here. Read more about how generate it
<a href="/help/ssh">here</a>
</p>
</div>
</div>
<div class='actions'>
<input class="btn btn-save" name="commit" type="submit" value="Save" />
<a href="/keys" class="btn btn-cancel">Cancel</a>
</div>
</form>

</div>

<script>
  $('#key_key').on('keyup', function(){
    var title = $('#key_title'),
        val      = $('#key_key').val(),
        key_mail = val.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+|\.[a-zA-Z0-9._-]+)/gi);

    if( key_mail && key_mail.length > 0 && title.val() == '' ){
      $('#key_title').val( key_mail );
    }
  });
</script>
</div>
</div>
</body>
</html>
4

2 回答 2

2

您正在寻找错误的元素。getElementsByTagName()正在寻找实际的标签名称( input),而不是标签的名称属性( commit) 的值。此外,getElementsByTagName()返回 COM 对象的集合。即使没有找到匹配的标签,该方法仍然会返回一个集合(包含 0 个元素)。您需要检查Length属性,然后访问集合的第一个元素:

$commit = $doc.getElementsByTagName("input")
if ($commit.Length -gt 0) {
  $commit.item(0).click()
}

或从集合中过滤具有您要查找的名称的元素:

$commit = $doc.getElementsByTagName("input") | ? { $_.name -eq "commit" }
if ($commit) { $commit.click() }
于 2013-07-18T11:53:52.817 回答
0

我无法访问您上面列出的 url,所以我使用 MIT 网站向您展示了如何做到这一点的示例。

# setup
$ie = New-Object -com InternetExplorer.Application 
$ie.visible=$true

$ie.navigate("http://web.mit.edu/") 
while($ie.ReadyState -ne 4) {start-sleep -m 100} 

$termsField = $ie.document.getElementsByName("terms")
@($termsField)[0].value ="powershell"


$submitButton = $ie.document.getElementsByTagName("input") 
Foreach($element in $submitButton )
{
    #look for this field by value this is the field(look for screenshot below) 
    if($element.value -eq "Search"){
    Write-Host $element.click()
    }
}

    Start-Sleep 10

在此处输入图像描述

于 2015-07-05T16:05:18.010 回答