问题
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