c#web浏览器中输入的占位符
本文关键字:占位符 输入 浏览器 c#web | 更新日期: 2023-09-27 18:25:11
有点奇怪,但在winforms c#中的web浏览器中似乎没有"占位符"。
在我的测试程序中,我试图通过网站的"占位符"获取特定的输入字段,并在字段中设置一个值。名称和类总是不同的,唯一的"footprint"是占位符。
例如,在一些网站的html中,有一个输入字段,代码如下:<input type="text" name="Username" placeholder="Username">
问题是通过查找占位符在字段中设置值。
我将输入元素与:HtmlElementCollection htmlcol = webBrowser1.Document.GetElementsByTagName("input");
并用循环htmlcol[i].GetAttribute("placeholder").Equals("Username").
搜索特定输入。不起作用
有关于如何解决这个问题的想法或例子吗?
感谢
首先需要获取容器元素,然后可以为其值设置值
例如,
HtmlElementCollection htmlcol = webBrowser1.Document.GetElementsByTagName("input");
foreach(HtmlElement he in htmlcol){
if(he.innerHtml.Contains("Username"))
he.SetAttribute("value",yourstring);
break;
}
经过两天的测试,我发现在通过Web浏览器访问时,innerHtml中没有任何用于输入字段内占位符的内容(除了名称、id、类和类似内容)。我尝试使用htmlcol[i].GetAttribute("placeholder").Equals("Username");
访问like,但由于某种原因,它不起作用。
对我有效的是OuterHtml,类似于以下内容:
HtmlElementCollection htmlcol = webBrowser1.Document.GetElementsByTagName("input");
for (int i = 0; i < htmlcol.Count; i++)
{
if (htmlcol[i].OuterHtml.Contains("Username"))
{
htmlcol[i].SetAttribute("value", pUsername.Text);
}
}
我希望我的回答能对解决这个问题的人有所帮助。
感谢