简体   繁体   中英

Web browser control to fill <input> field

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", "example@example.com");

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM