硒2 +另一个文件上传

发布于 2024-11-02 05:10:56 字数 1665 浏览 0 评论 0原文

嗯,我知道硒世界充满了文件上传线程,这是我今天遇到的但到目前为止还无法解决的问题。虽然过去通过使用FF浏览器在文件上传的文件输入文本框中输入来解决这些问题。

所以首先没有文件输入框。只需一个按钮,就会弹出一个窗口来选择文件,一旦您选择了文件,上传就会开始。 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/name 等来单击但无济于事。我尝试了这样的点击 -

Commons.clickById(webDriver, "pickfiles")

但页面上没有任何反应。

我也尝试过 - 此处发布的代码片段使用 java 脚本执行 -

无法点击按钮打开文件附件对话框

但无济于事。我总是遇到错误提示 -

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

有什么建议吗?

Well I know that selenium world is full of file upload threads, and this is something which I came across today and have not been able to solve so far. Though have solved these issues in past by typing in the file input text box of file upload using FF browser.

So first of all there is no file input box. It is just one button which brings a pop up to select a file and as soon as you pick the file, upload begins own its on. html looks as -

<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>

So I tried using id/name of etc to click but of no avail. I tried clicks like these -

Commons.clickById(webDriver, "pickfiles")

But nothing happens on page.

I also tried - code snippet posted here which uses java script exectuion -

cant click button which opens file attachment dialog

but of no avail. I always encounter error stating -

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

Any Suggestion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜尕妞 2024-11-09 05:10:56

并使用 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}");

我希望它对其他人有帮助

And got the solution using autoit, here is the sample script -

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

I hope it helps others

内心旳酸楚 2024-11-09 05:10:56

首先,我必须创建一个小类来找出哪些窗口打开,并将它们作为窗口字典返回。

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

键下载窗口应该关闭,因此我们切换回浏览器窗口(在本例中为 Firefox)。

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

这有几个问题,因为窗口标题根据所使用的浏览器而不同,因此需要考虑到这一点一个完整的解决方案。此外,当执行这部分代码时,不要将任何其他窗口带到前台,因为该窗口将接收“SendKeys”而不是所需的窗口。

First of all I had to create a small class to find out which windows were open and return them as a dictionary of windows.

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

In the Selenium page code I click on the button to launch the download window and put in a short wait. (This is not shown in the code)

Then I use the code below to find all open windows

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

Switch to the download window (the name of the window is different across browsers. Be Aware!)

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

Type in the path for the file

SendKeys.SendWait(filename);

Press Enter

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

The download window should close so we switch back to the browser window (in this case Firefox)

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

There are a couple of issues with this as the window titles are different depending on which browser is being used, so this will need to be taken into account for a complete solution. In addition, when this section of code is executing do not bring any other windows to the foreground as this window will receive the 'SendKeys' rather than the required window.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文