2011-04-19 66 views
0

那么我知道硒世界充满了文件上传线程,这是我今天碰到的并且到目前为止还没有能够解决的问题。尽管通过使用FF浏览器输入文件上传的文件输入文本框来解决了这些问题。Selenium2 +另一个文件上传

所以首先没有文件输入框。它只是一个按钮,它会弹出一个选择文件的文件,只要您选择文件,上传就会自动启动。 HTML看起来像 -

<div id="container" style="position: relative;"> 
     <div id="filelist"></div> 
     <br> 
     <a id="pickfiles"> 
     <input type="button" name="Photos" value="Pick a File"></a> 
     <div id="p15tlsibt1185d1pi41tbd16c31a0n0_flash_container" style="position: absolute; top: 21px; background: none repeat scroll 0% 0% transparent; z-index: 99999; width: 86px; height: 18px; left: 0px;" class="plupload flash"><object width="100%" height="100%" data="/CKFinder/upload/content/runtimes/plupload.flash.swf" type="application/x-shockwave-flash" style="outline: 0pt none; background-color: transparent;" id="p15tlsibt1185d1pi41tbd16c31a0n0_flash"><param value="/CKFinder/upload/content/runtimes/plupload.flash.swf" name="movie"><param value="id=p15tlsibt1185d1pi41tbd16c31a0n0" name="flashvars"><param value="transparent" name="wmode"><param value="always" name="allowscriptaccess"></object></div></div> 

所以我尝试使用id /名称等点击,但无济于事。我试过这样的点击 -

Commons.clickById(webDriver, "pickfiles") 

但是页面上没有任何反应。

我也尝试 - 代码片段贴在这里,它使用Java脚本exectuion -

cant click button which opens file attachment dialog

,但无济于事。我总是遇到错误陈述 -

System.InvalidOperationException : arguments[0].click is not a function (UnexpectedJavaScriptError) 

有什么建议吗?

+0

我能够点击按钮,因为我之前通过错误的会话是错误的。尽管点击按钮并不能解决问题,因为我需要在弹出的窗口中点击文件进行选择,并且文件结构在机器之间不会保持不变。我想用后台调用来执行文件上传,可能会使用HTMLUnit而不是浏览器。但是,但我需要选择文件....一些如何... – Tarun 2011-04-19 09:13:49

+0

看起来像我非常严重坚持这一点, 有没有输入框键入文件路径和选择文件从弹出窗口是非常糟糕的执行:( – Tarun 2011-04-19 13:56:56

回答

0

,并得到使用的AutoIt的解决方案,这里是示例脚本 -

AutoItX3Lib.AutoItX3 au3 = new AutoItX3Lib.AutoItX3(); 
au3.WinWait("Select file to upload"); 
au3.WinActivate("Select file to upload"); 
au3.Send("C:\\Documents and Settings\\user\\Desktop\\area51.png"); 
au3.Send("{ENTER}"); 

我希望它可以帮助别人

+0

你可以请发表完整的代码,你如何得到它的工作:)?我有一些问题和完全相同的情况 – Mihai 2013-04-04 09:55:11

0

首先我必须创建一个小班找出哪个窗口是开放的并将它们作为窗口字典返回。

public static IDictionary<string, IntPtr> GetOpenWindows() 
{ 
    IntPtr lShellWindow = GetShellWindow(); 
    Dictionary<string, IntPtr> lWindows = new Dictionary<string, IntPtr>(); 
    EnumWindows(delegate(IntPtr hWnd, int lParam) 
    { 
     if (hWnd == lShellWindow) return true; 
     if (!IsWindowVisible(hWnd)) return true; 
     int lLength = GetWindowTextLength(hWnd); 
     if (lLength == 0) return true; 

     StringBuilder lBuilder = new StringBuilder(lLength); 
     GetWindowText(hWnd, lBuilder, lLength + 1); 
     lWindows[lBuilder.ToString()] = hWnd; 
     return true; 
    }, 0); 

    return lWindows; 
} 

public delegate bool EnumDelegate(IntPtr hWnd, int lParam); 
public delegate bool EnumedWindow(IntPtr handleWindow, ArrayList handles); 

[DllImport("USER32.DLL")] 
public static extern bool EnumWindows(EnumDelegate enumFunc, int lParam); 

[DllImport("USER32.DLL")] 
public static extern IntPtr GetShellWindow(); 

[DllImport("USER32.DLL")] 
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); 

[DllImport("USER32.DLL")] 
public static extern int GetWindowTextLength(IntPtr hWnd); 

[DllImport("USER32.DLL")] 
public static extern bool IsWindowVisible(IntPtr hWnd); 

[DllImport("user32.dll")] 
public static extern bool SetForegroundWindow(IntPtr hWnd); 

在Selenium页面代码中,我点击按钮启动下载窗口,并放入一个短暂的等待。 (这不是代码所示)

然后我用下面的代码查找所有打开的窗口

IDictionary<string, IntPtr> getOpenWindows = GetOpenWindows(); 

切换到下载窗口(窗口的名称是跨浏览器的不同。注意! )

IntPtr hWnd = getOpenWindows["File Upload"]; 
SetForegroundWindow(hWnd); 

路径键入文件

SendKeys.SendWait(filename); 

按Enter

SendKeys.SendWait(@"{Enter}"); 

的下载窗口应关闭,所以我们切换回浏览器窗口(在这种情况下,火狐)

hWnd = getOpenWindows["Mozilla Firefox"]; 
SetForegroundWindow(hWnd); 

有几个问题以此为窗口标题,这取决于不同浏览器正在被使用,所以这需要考虑到一个完整的解决方案。此外,当这部分代码执行时,不要将任何其他窗口置于前台,因为此窗口将接收'SendKeys'而不是所需的窗口。

相关问题