问题
I am trying to set a value programatically to a textbox in a web browser but it is throwing the Null exception.
This is the code, I have tried:-
webBrowser1.Document.GetElementById("field-summary").InnerText = "Something";
webBrowser1.Document.GetElementById("field-summary").SetAttribute("value", "Something");
I have tried to find the element by using the Quick Watch and it is there, but I am unable to set a value.
Hereby, I have attached the snap of watch too.
And this is the error, I am getting.
A first chance exception of type 'System.NullReferenceException' occurred in TimeSheet.exe A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
My question is I am trying to set a value to the innertext and I am not doing any manipulation with the InnerText. But still it is throwing error.
I have tried both InnerText, value too. But still unable.
I have also attached the snap, using the Inspect for reference.
PS: 1. All these code set are done in the DocumentCompleted Event of the WebBrowser, but still unable.. 2. I have accessed other elements (text) in some other pages and used the InnerText for accessing and setting a value for it. But unable to do with this element.
Could some one throw a light on why it is doing so, so I can able to understand it.
Thanks.
回答1:
May be your elements have not loaded yet. Do this action in window load event handler. You can use Jquery events.
回答2:
Found a working solution for my problem from SO,.
How to get HTML textbox value?
So, the following code worked for me.
dynamic elePrice = webBrowser.Document.GetElementById("price").DomElement; string sValue = elePrice.value;
Thanks.
来源:https://stackoverflow.com/questions/41323673/unable-to-set-value-to-textbox-control-programatically-in-webbrowser