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 codeid="${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
2 Answers
Reset to default 3Try 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;
}
}
}