输入用户名、密码后,点击登录无效?


PowerShell交流中心输入用户名、密码后,点击登录无效?
-1
liusheng asked 4年 ago

网站登录部分源码如下:

<form id=”fm1″ class=”fm-v clearfix” action=”/summer-ssoserver/login?service=http%3A%2F%2F145.2.2.101%3A8080%2Fsym%2Flogout&amp;tgt=null” method=”post”>
<div class=”login”>

<div class=”login_con_bg”>
<img class=”fd-img fd-login-con-bg” src=”images/icon-update/icon-bg-left.png” />
<img class=”fd-img fd-before” src=”images/icon-update/icon-bg-before.png” />
<img class=”fd-img fd-after” src=”images/icon-update/icon-bg-after.png” />
<div class=”login_con”>
<div class=”l_con”>
<div class=”fd-title”>华宇应用平台
<img class=”fd-img fd-title-bg” src=”images/icon-update/icon-title-bg.png” />
</div>
<span id=”download_error” class=”error_download”></span>

<div class=”dl_con”>
<div class=”fd-form-row”>
<span class=”fd-label”>
<img class=”fd-img fd-label-bg” src=”images/icon-update/icon-user.png” />
</span>
<input id=”username1″ type=”text” class=”dl_input” onblur=”fEvent(‘blur’,this);usernameOnBlur(beforeGetScopeCallBack,afterGetScopeCallBack);” onfocus=”fEvent(‘focus’,this)” placeholder=”请输入您的用户名”>
<input name=”username” id=”username” type=”hidden”>
</div>
<div class=”fd-form-row”>
<span class=”fd-label fd-pw”>
<img class=”fd-img fd-pw-bg” src=”images/icon-update/icon-pw.png” />
</span>
<input id=”password1″ type=”password” class=”dl_input” placeholder=”请输入您的密码”>
<input name=”password” id=”password” type=”hidden”>
</div>
<input name=”accountScope” id=”accountScope_input” style=”display: none”/>
<div class=”fd-form-row fd-hide” id=”accountScope”>
<span id=”login_corp” class=”fd-label fd-place”>
<img class=”fd-img fd-place-bg” src=”images/icon-update/icon-place.png” />
</span>

<div class=”search_menu_sub js_search_menu_sub” id=”innerAccountScope”>
<div class=”search_value_box_sub”>
<span class=”sp_mousedown” id=”sp_mousedown1″></span>
<span class=”sp_gain_value” value=”请选择单位”>请选择单位</span>
</div>
<!– search_value_box end –>
<dl class=”search_con_nav_sub”>
</dl>
</div>
</div>
<input name=”authenticated” id=”authenticated” class=”dl_input”
style=”display: none”>
</input>
<input name=”userId” id=”userId” class=”dl_input”
style=”display: none”>
</input>
<input name=”scope” id=”scope” class=”dl_input”
style=”display: none”>
</input>

<div class=”dl_cz”>
<img class=”fd-img fd-btn-bg” src=”images/icon-update/icon-btn.png” />
<a href=”javascript:formLogin();” class=”submit_btn”>登录</a>
</div>
<div class=”error_td”>

<div id=”tips” class=”tips” align=”left” style=”display: none;font-size:11px;”>系统检测您已从其他地方登录或超时登录</div>
</div>
</div>
<input type=”hidden” name=”lt” value=”_cB35B36C5-3F2A-3F19-4DEC-68B976E2757C_kA327FF05-2A16-D90D-3303-0B9CDBF9E68A”/>
<input type=”hidden” name=”_eventId” value=”submit”/>
</div>
</div>
</div>

<div class=”login_foot”><p></p></div>
</div>

<input type=”hidden” id=”isSafeEnv” value=”2″>
</input>
</form>

代码是这样的。

$ie =  New-Object -ComObject InternetExplorer.Application

$ie.Navigate(“XXXX”)

$ie.Visible = $true

while ($ie.Busy) {Start-Sleep -Second 1}

$ie.Document.GetElementById(“username1”) = “XXXX”

$ie.Document.GetElementById(“password1”) = “XXXX”

($ie.Document.GetElementsByTagName(“a”) | Where-Object {$_.ClassName -eq “submit_btn”}).Click()

执行以上代码的结果是:用户名、密码被填充后,登录页面闪烁一下,然后密码框被清空,页面仍停留在登录页而没有登入跳转。当我只执行填充用户名、密码的代码,而不执行点击登录Click()的操作时,用户名和密码框均被填充,此时我尝试手动点击登录,同样出现页面闪烁后清空密码框并停留在登录页的情况,请问我该怎么才能登入网站?首次尝试网页自动化,很多不懂,请指教啊!

 

1 Answers
1
Best Answer
Mooser Lee 管理员 answered 4年 ago

如果整个网页是干净的form表单,IE automation 这种方式是能行的通。但是一旦表单中的输入框,加入一些自定义事件逻辑,可能就会失败
为什么会失败? 因为$ie.Document.GetElementById(“username1”)  这是一种暴力替换值的方式,没有经过文本框设置焦点事件,键盘敲击事件,焦点离开事件,这样会导致客户端某些依赖的事件没有被触发,但是网页上却写了相关的事件:“onblur” 的处理,自然会有问题。
推荐使用:  https://www.pstips.net/selenium-powershell-extensions.html
因为他的文本框设置值,其实模拟了一个:聚焦,send key,失焦的完整流程,自然不会遇到上面提到的问题。
 

liusheng replied 4年 ago

谢谢,根据你的指导,我去研究研究。

vs-wlw_422 replied 4年 ago

谢谢,根据你的指导,我去研究研究。