我是 Angular 的新手,我需要通過注入 JS(Winform Chromium 控制元件)從瀏覽器外部填充此表單的輸入。
<form ng-if="!loginController.authentication.formsDisabled" novalidate="" class="css-form ng-pristine ng-valid ng-scope" name="form">
<input type="text" tabindex="1" placeholder="Username" ng-model="loginController.user.name" name="username" autofocus="autofocus" autocomplete="off" autocapitalize="off" autocorrect="off" spellcheck="false" ng-trim="false">
<input type="password" tabindex="2" placeholder="Password" autocomplete="off" ng-model="loginController.user.password" name="password">
<!-- ngIf: loginController.errorInformation -->
<!-- ngIf: !loginController.allowSaveInformation -->
<div ng-show="loginController.allowSaveInformation">
<input id="remember_password_option" tabindex="3" type="checkbox" name="remember_password" ng-model="loginController.user.saveLoginInformation" >
<label for="remember_password_option" ><span></span>Keep me logged in</label>
</div>
<button ng-click="loginController.login()" tabindex="4">
<div ng-hide="loginController.loginPending" >Log in</div>
<div ng-show="loginController.loginPending" >
<!-- ngInclude: --><ng-include src="'spinnerTransparent.tpl.html'" ><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 70 70" ><g fill="currentColor"> <circle cx="34.5" cy="7.5" r="6.5"></circle> <circle cx="55.5" cy="15.5" r="6.5"></circle> <circle cx="62.5" cy="35.5" r="6.5"></circle> <circle cx="55.5" cy="55.5" r="6.5"></circle> <circle cx="34.5" cy="62.5" r="6.5"></circle> <circle cx="14.5" cy="55.5" r="6.5"></circle> <circle cx="7.5" cy="35.5" r="6.5"></circle> <circle cx="14.5" cy="15.5" r="6.5"></circle></g></svg></ng-include>
</div>
</button>
</form>
澄清一下-我無法控制表單的輸入方式。但是一旦加載,我可以注入我能想到的任何 JS。所以我這樣做:
@"document.querySelector('[placeholder=Username]').value='" username "'; "
@"document.querySelector('[placeholder=Password]').value='" password "'; "
填充輸入,我確實看到瀏覽器控制元件中的那些已填充。但是當我單擊他的登錄按鈕時,它會報告無效的用戶名或密碼。同時,如果我像用戶一樣在這些輸入中鍵入相同的用戶名和密碼,它確實會登錄。事實上,即使我向它們添加一個空格然后洗掉它登錄的那個空間。這是否與 Angular 的方式有關事情嗎?或者這應該有效嗎?我是否可能需要做一些特別的事情來更新這些東西:
ng-pristine ng-untouched ng-valid ng-empty
我發現了一些呼叫 setValue() 方法的 Angular 示例,但似乎沒有定義 setValue。form.controls 也沒有定義。
uj5u.com熱心網友回復:
嘗試在輸入值后分派輸入事件,以便稍后在提交時將其拾取:
@"document.querySelector('[placeholder=Username]').value='" username "'; "
@"document.querySelector('[placeholder=Username]').dispatchEvent(new Event('input'));
@"document.querySelector('[placeholder=Password]').value='" password "'; "
@"document.querySelector('[placeholder=Password]').dispatchEvent(new Event('input'));
然后以同樣的方式提交:
@"document.forms[0].dispatchEvent(new Event('submit'));
或點擊提交按鈕
@"document.querySelector('button').click()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/524414.html
