如何获取HTML文本框的值? [英] How to get HTML textbox value?

查看:759
本文介绍了如何获取HTML文本框的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在C#WinForms应用程序中获取网站上的几个文本框的值。
它是输入类型文本,设置为只读,但是当我尝试在应用程序中使用

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");

它返回一个空字符串。
当我尝试使用 .SetAttribute(value,testValue)设置其他(而非只读)输入值时,它的工作原理很好。

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?

推荐答案

无法使用 GetAttribute 检索文本框。

您可以使用以下代码检索值:

You can use the following code to retrieve the value:

dynamic elePrice = webBrowser.Document.GetElementById("price").DomElement;
string sValue = elePrice.value;

如果您无法使用 dynamic (即.NET 4+),那么您必须从Visual Studio的COM选项卡中引用'Microsoft HTML Object Library',并使用以下命令:

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;

编辑:
已使用以下代码进行测试:

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);
};

这篇关于如何获取HTML文本框的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆