0

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();
                        }
                    }
  }
};
Samuel
  • 35
  • 4

1 Answers1

1

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.

Vladyslav Tsvek
  • 244
  • 1
  • 10
  • Thank you the clarification and example code. Is there a way to get the frameId of an IFRAME without using myBrowser.GetFramesIds() ? because when I use GetFramesIds() I only get the frameId but don't know which IFRAME corresponds to it. I was exaggerating with the 1-2 seconds delay, but there is a delay when setting values to elements. To see the delay you can create a timer with 2 second interval and in the Timer_Elapsed event you can set the value for ex. 8 input elements. It's no problem. – Samuel Aug 29 '17 at 16:01
  • You can use property FrameId from the FinishLoadingEventArgs to know what a frame was loaded. I reproduced the described issue with a delay. It will be fixed in one of the next releases. – Vladyslav Tsvek Sep 01 '17 at 16:14