最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Web browser control to fill <input> field - Stack Overflow

programmeradmin1浏览0评论

I'm trying to open a web page in my webbrowser control and change the value of input fields. Works good when I'm doing it like this webBrowser.Document.GetElementById("Email").SetAttribute("value", "[email protected]");

on a page with defined element Ids, but now I've encountered a page where the html/javascript looks something like this:

<input id="${Id}" name="${Id}" type="text" class="text field" value="${Value}" title="${ToolTip}" />

So my question is how do I find this specific input field from the C# code?

I'm trying to open a web page in my webbrowser control and change the value of input fields. Works good when I'm doing it like this webBrowser.Document.GetElementById("Email").SetAttribute("value", "[email protected]");

on a page with defined element Ids, but now I've encountered a page where the html/javascript looks something like this:

<input id="${Id}" name="${Id}" type="text" class="text field" value="${Value}" title="${ToolTip}" />

So my question is how do I find this specific input field from the C# code?

Share Improve this question edited Jun 12, 2014 at 7:27 rene 42.5k78 gold badges121 silver badges165 bronze badges asked Jun 12, 2014 at 7:20 isakfuisakfu 742 silver badges7 bronze badges 4
  • I guess you have to find what is the value of ${Id} in the code id="${Id}", then you will be able to call your getElementById() method – user3241019 Commented Jun 12, 2014 at 7:27
  • 1 Looks like a javascript template engine needs to run first... – rene Commented Jun 12, 2014 at 7:28
  • Are you using webbrowser control in web application? – Golda Commented Jun 13, 2014 at 5:38
  • I'm using Windows Forms @Golda – isakfu Commented Jun 13, 2014 at 12:06
Add a ment  | 

2 Answers 2

Reset to default 3

Try This

This is a C# coding

HtmlElement Elem = AutomationWebBrowser.Document.GetElementById('<your element ID>');
Elem.SetAttribute("value", '<value to assign in input control>');

Also you can use variables inside the GetElementById and SetAttribute function

You can find element by class name by using the following function,

public static HtmlElement GetHTMLElementByClass(HtmlDocument document, String className)
{
    foreach (HtmlElement element in document.All)
    {
        if (element.GetAttribute("className") == className)
        {
            return element;
        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论