used powershell to do web ui automation. came up an exception: invoke method failed, because [System.__ComObject] does not contain “click” method.
can submit type input be clicked?
i used getElementsByTagName getElementsByClassName getElementsByName , does not work.
anyone can help me on this?
powershell code is below:
# 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()}
the html source is as below:
<!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="✓" /></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&d=mm" src="http://www.gravatar.com/avatar/f3ea5164088694b48e4980e52d831927? s=26&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="✓" /><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>
You're looking for the wrong element. getElementsByTagName()
is looking for the actual tag name (input
), not the value of the tag's name-attribute (commit
). Also, getElementsByTagName()
returns a collection of COM objects. Even if no matching tag is found, the method will still return a collection (with 0 elements). You need to either check the Length
property and then access the first element of the collection:
$commit = $doc.getElementsByTagName("input")
if ($commit.Length -gt 0) {
$commit.item(0).click()
}
or filter the element with the name you're looking for from the collection:
$commit = $doc.getElementsByTagName("input") | ? { $_.name -eq "commit" }
if ($commit) { $commit.click() }
I could not access the url you had listed above so I used the MIT website to show you an example of how can this be done.
# 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
来源:https://stackoverflow.com/questions/17721295/powershell-how-to-click-a-submit-type-input