Using WebBrowser to automate filling in an AngularJS form

两盒软妹~` 提交于 2019-12-12 01:41:35

问题


I am using WebBrowser to automate filling in a textbox, on a website that uses AngularJS.

This is the HTML on their website

<input name="email" type="email" placeholder="Email address" ng-model="login.email" autofocus="" class="ng-pristine ng-valid ng-empty ng-valid-email ng-touched" aria-invalid="false">

This is my C# code

webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("email")[0].SetAttribute("value", _login);

That populates the input box, but I don't think it updates the Angular model.
Hence you can't submit the form because that website (ie. Angular) thinks it is still empty.

How do you trigger the ng-model update from my WebBrowser control?


回答1:


This is the script needed to update ng-model

angular.element($("input[name='email']")).triggerHandler('change')

and you can execute it from WebBrowser like this

wb.Document.InvokeScript("eval", new[] { "angular.element($('input[name=email]')).val('your@email').triggerHandler('change')" });


来源:https://stackoverflow.com/questions/43207999/using-webbrowser-to-automate-filling-in-an-angularjs-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!