2017-08-25 58 views
0

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

将值设置为元素时,会有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(); 
         } 
        } 
    } 
}; 

回答

1

要考虑到你不能让一个点击通过点击()方法FileUpload对象上由于安全限制:

如果算法不被用户激活触发,则中止这些 步骤没有做任何事情。

该规范包含有关文件上传算法的详细信息:https://html.spec.whatwg.org/multipage/input.html#file-upload-state-(type=file)

下面是一个说明如何使用输入工作的www.cs.tut.fi 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"); 

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

+0

谢谢你的澄清和示例代码。 有没有办法获得IFRAME的frameId而不使用myBrowser.GetFramesIds()?因为当我使用GetFramesIds()时,我只获取frameId,但不知道哪个IFRAME对应于它。 我夸大了1-2秒的延迟,但在设置元素值时会有延迟。要查看延迟,您可以创建一个具有2秒间隔的计时器,并在Timer_Elapsed事件中可以设置ex的值。 8个输入元素。 这是没有问题的。 – Samuel

+0

示例代码:https://pastebin.com/PPQ8kQh8 – Samuel

+0

您可以使用FinishLoadingEventArgs中的属性FrameId来了解加载的框架。我耽搁了所描述的问题。它将在下一个版本中修复。 –