【问题标题】:c# Placeholder from input in web browserc#来自Web浏览器输入的占位符
【发布时间】:2014-11-25 12:39:52
【问题描述】:

有点奇怪,但在winforms c#的网络浏览器中似乎没有“占位符”。

在我的测试程序中,我试图通过它的“占位符”从网站获取特定的输入字段并在该字段中设置一个值。名称和类总是不同的,唯一的“足迹”是占位符。

例如在某些网站的 html 中,有这样的代码的输入字段: <input type="text" name="Username" placeholder="Username"> 问题是通过查找占位符在字段中设置一个值。

我将输入元素与: HtmlElementCollection htmlcol = webBrowser1.Document.GetElementsByTagName("input"); 并使用循环搜索特定输入 htmlcol[i].GetAttribute("placeholder").Equals("Username"). 它只是不工作

关于如何解决问题的任何想法或示例?

谢谢

【问题讨论】:

  • 你问的是什么?
  • 他想通过查找具有指定值的placeholder 属性来获取元素...他的问题很清楚
  • 也许我的问题不够清楚,可能会令人困惑。例如,在某些网站的html中,有这样的代码输入字段: 问题是通过查找占位符在字段中设置一个值。希望你现在能理解我:)
  • @BombaRuLz 好的,在你澄清后我恢复了 -1。

标签: c# winforms browser placeholder


【解决方案1】:

经过 2 天的测试,我发现在通过 webbrowser 访问时,innerHtml 中没有任何内容用于输入字段内的占位符(名称、id、类和类似内容除外)。我尝试使用htmlcol[i].GetAttribute("placeholder").Equals("Username"); 进行访问,但由于某种原因它没有奏效。

对我有用的是 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);
        }
}

我希望我的回答能帮助解决这个问题。

谢谢

【讨论】:

    【解决方案2】:

    首先你需要获取那个容器元素然后你可以为它的值设置值

    例如,

            HtmlElementCollection htmlcol = webBrowser1.Document.GetElementsByTagName("input");
    
            foreach(HtmlElement he in htmlcol){
            if(he.innerHtml.Contains("Username"))
    
            he.SetAttribute("value",yourstring);
            break;
            }
    

    【讨论】:

    • 我已经使用HtmlElementCollection htmlcol = webBrowser1.Document.GetElementsByTagName("input"); 获取输入元素,并使用循环htmlcol[i].GetAttribute("placeholder").Equals("Username") 搜索特定输入。它只是不工作
    • 您的代码应该像我的一样工作,但问题是,由于未知原因,出现了空异常,就像 Web 浏览器中不存在占位符一样。干杯
    • 好的,然后在if语句之前检查元素是否不为null...就像这样 if(he!=null){ if(he.innerHtml...}
    猜你喜欢
    • 2018-11-10
    • 2013-03-19
    • 2014-01-12
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 2011-07-08
    • 1970-01-01
    相关资源
    最近更新 更多