0

当我尝试以编程方式单击输入元素(type="file")时,不会出现 ChooseFileDialogWindow。通过尝试单击“开始上传”,可以在http://imgbb.com/上重新创建相同的问题。在这个网站上它只适用于 SimulateMouseButtonEvent,在 www.cs.tut.fi 上它不起作用。

为元素设置值时,在移动到下一个元素之前会有 1-2 秒的延迟。在 IE 中,这将立即执行。当浏览器关注元素时,这是在 porpuse 上完成的吗?有没有办法禁用它?

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

1 回答 1

1

考虑到由于安全限制,您无法通过 FileUpload 对象上的 Click() 方法单击:

如果算法不是由用户激活触发的,则中止这些步骤而不做任何其他事情。

本规范包含有关 FileUpload 算法的更多信息:https://html.spec.whatwg.org/multipage/input.html#file-upload-state-(type=file)

这是一个代码示例,演示了如何使用 www.cs.tut.fi 上的 Input FileUpload 对象:

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");

我无法检测到您或我的代码示例有任何延迟。

于 2017-08-29T13:16:04.980 回答