Search code examples
wpfdotnetbrowser

Clicking on input element (FileUpload)


When I try to programmatically click on an input element (type="file"), ChooseFileDialogWindow doesn't appear. The same problem can be recreated on http://imgbb.com/ by trying to click on 'Start Uploading'. On this website It works only with SimulateMouseButtonEvent, on www.cs.tut.fi it doesn't work.

When setting values to elements there is 1-2 seconds delay until it moves on to the next element. In IE this will be executed instantly. Is this done on porpuse when the browser focuses on the element ? Is there a way to disable it?

browser.LoadURL("http://www.cs.tut.fi/~jkorpela/forms/file.html");

browserView.Browser.FinishLoadingFrameEvent += delegate(object sender, FinishLoadingEventArgs e)
{
  if (e.IsMainFrame)
  {
    Browser myBrowser = e.Browser;
    DOMDocument document = myBrowser.GetDocument();

                    foreach (DOMElement el in document.GetElementsByTagName("input"))
                    {
                        if (el.GetAttribute("name") == "datafile")
                        {
                            el.Focus();
                            el.Click();
                        }
                    }
  }
};

Solution

  • Take into account that you can't make a click by Click() method on FileUpload object because of the security restriction:

    If the algorithm is not triggered by user activation, then abort these steps without doing anything else.

    This specification contains more information about FileUpload algorithm: https://html.spec.whatwg.org/multipage/input.html#file-upload-state-(type=file)

    Here is a code sample that demonstrates how to work with Input FileUpload object on www.cs.tut.fi:

    browserView.Browser.FinishLoadingFrameEvent += delegate (object sender, FinishLoadingEventArgs e)
    {
        if (e.IsMainFrame)
        {
            Browser myBrowser = e.Browser;
            DOMDocument document = myBrowser.GetDocument();
    
            foreach (DOMElement el in document.GetElementsByName("datafile"))
            {
                el.Focus();
                System.Drawing.Rectangle rect = el.BoundingClientRect;
                Dispatcher.Invoke(() =>
                {
                    browserView.InputSimulator.SimulateMouseButtonEvent(MouseButton.Left,
                                        MouseButtonState.Pressed, 1,
                                        rect.Left + (el.ClientWidth / 2),
                                        rect.Top + (el.ClientHeight / 2));
                    Thread.Sleep(50);
    
                    browserView.InputSimulator.SimulateMouseButtonEvent(MouseButton.Left,
                                        MouseButtonState.Released, 1,
                                        rect.Left + (el.ClientWidth / 2),
                                        rect.Top + (el.ClientHeight / 2));
                });
            }
        }
    };
    browserView.Browser.LoadURL("https://www.cs.tut.fi/~jkorpela/forms/file.html");
    

    I can't detect any delay with your or my code samples.