Search code examples
c#dotnetbrowser

How to fill in username and password using DotNetBrowser


public MainWindow()
{
    InitializeComponent();

    BrowserView webView = new WPFBrowserView();
    mainLayout.Children.Add((UIElement)webView.GetComponent());
    ManualResetEvent waitEvent = new ManualResetEvent(false);
    webView.Browser.FinishLoadingFrameEvent += delegate (object sender, FinishLoadingEventArgs e)
    {
        if (e.IsMainFrame)
        {
            waitEvent.Set();
        }
    };
    webView.Browser.LoadURL("https://console.api.ai/api-client/#/login");
    waitEvent.WaitOne();
    DOMDocument document = webView.Browser.GetDocument();
    DOMElement username = document.GetElementById("username");
    username.SetAttribute("value", "[email protected]");
}

This is my program which navigate to "https://console.api.ai/api-client/#/login".

I trying to fill "[email protected]" into the Email textbox in the website using .SetAttribute but it doesn't work.

Anyone know how to solved this?

Thanks!


Solution

  • In the provided sample code you are trying to modify element property via the element attribute. Element attributes cannot be used to modify element properties: What is the difference between properties and attributes in HTML?

    Here is a sample code that demonstrates how to use the Value property for setting the input field value:

    DOMInputElement username = (DOMInputElement)document.GetElementById("username");
    username.Value = "[email protected]";
    

    The sample by the following link demonstrates how to work with various form fields: https://dotnetbrowser.support.teamdev.com/support/solutions/articles/9000110038-setting-input-field-value-working-with-form