2

I just try to understand the concept of in web browser control c# win app.

I have created a Windows app with web browser control. I have called a web page (which is my own) and try to set value in the web page from my app.

When i try with Google

webBrowser1.Navigate("http://google.com/search?q=" + "C#");

it works fine.

When i try in this way, it is not working.

HtmlElement textArea = webBrowser1.Document.All["q"];
textArea.InnerText = "dsfs";

Can anyone help me to achieve this?

1
  • What do you want to achieve ? Do you want to set a value for query string parameter "q" ? Commented May 25, 2015 at 4:21

1 Answer 1

2

You will need to wait for the WebBrowser to load before you access it (otherwise Document will be null until loaded) - subscribe a DocumentCompleted event handler for this. Also Document.All["q"] will return the first element with the name "q".

webBrowser1.Navigate("http://stackoverflow.com/questions/30431004");
webBrowser1.DocumentCompleted += (o, args) =>
{
    var ele = webBrowser1.Document.All["q"];
    if (ele.TagName.ToLower() == "input")
    {
        ele.InnerText = "dsfs";
    }
};

If you want to change more than one such element, or if you want to locate elements by Id, Tag name etc, you will need to iterate the collection:

foreach (HtmlElement ele in webBrowser1.Document.All)
{
    if (ele.TagName.ToLower() == "input")
    {
        ele.InnerText = "dsfs";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.