开发者

Selenium2 + Yet another file upload

开发者 https://www.devze.com 2023-02-26 11:07 出处:网络
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 typin

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 fil开发者_运维知识库e attachment dialog

but of no avail. I always encounter error stating -

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

Any Suggestion?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消