I am trying to get values of few textboxes on a website in a C# WinForms app.
It is input type text, set to readonly, but when I try to read it in my app using
string price = webBrowser1.Document.GetElementById("price").GetAttribute("value");
it returns an empty string.
When I try to set some other (not readonly) input value using .SetAttribute("value", "testValue")
, it works just fine.
Anyone could help me with this please?
Unfortunately the value from the text box cannot be retrieved using GetAttribute
.
You can use the following code to retrieve the value:
dynamic elePrice = webBrowser.Document.GetElementById("price").DomElement;
string sValue = elePrice.value;
If you are not able to use dynamic
(i.e. .NET 4+) then you must reference the 'Microsoft HTML Object Library' from the COM tab in Visual Studio and use the following:
mshtml.IHTMLInputElement elePrice = (mshtml.IHTMLInputElement)webBrowser.Document.GetElementById("price").DomElement;
string sValue = elePrice.value;
EDIT:
This has been tested with the following code:
webBrowser.Url = new Uri("http://files.jga.so/stackoverflow/input.html");
webBrowser.DocumentCompleted += (sender, eventArgs) =>
{
var eleNormal = (IHTMLInputElement)webBrowser.Document.GetElementById("normal").DomElement;
var eleReadOnly = (IHTMLInputElement)webBrowser.Document.GetElementById("readonly").DomElement;
var eleDisabled = (IHTMLInputElement)webBrowser.Document.GetElementById("disabled").DomElement;
MessageBox.Show(eleNormal.value);
MessageBox.Show(eleReadOnly.value);
MessageBox.Show(eleDisabled.value);
};
try to use innertext
string price = webBrowser1.Document.GetElementById("price").InnerText;