如何使用Selenium WebDriver处理Windows文件上传?

Jas*_*vra 41 java selenium file-upload webdriver selenium-webdriver

我在Stackoverflow上使用Selenium WebDriver看到了很多关于文件上传的问题和解决方案.但是没有一个适用于以下场景.

有人给出了如下解决方案

// assuming driver is a healthy WebDriver instance
WebElement fileInput = driver.findElement(By.name("uploadfile"));
fileInput.sendKeys("C:/path/to/file.jpg");
Run Code Online (Sandbox Code Playgroud)

但我还是找不到窗口句柄我该如何处理?

截图

我正在寻找上述方案的解决方案

请检查以下任何网站

http://www.uploadify.com/demos/
http://www.zamzar.com/
Run Code Online (Sandbox Code Playgroud)

Pet*_*ček 43

// assuming driver is a healthy WebDriver instance
WebElement fileInput = driver.findElement(By.name("uploadfile"));
fileInput.sendKeys("C:/path/to/file.jpg");
Run Code Online (Sandbox Code Playgroud)

嘿,这是我的某个地方:).


对于Zamzar网络,它应该完美地工作.您不要单击该元素.你只需输入它的路径.具体来说,这应该是绝对可以的:

driver.findElement(By.id("inputFile")).sendKeys("C:/path/to/file.jpg");
Run Code Online (Sandbox Code Playgroud)

Uploadify网站的情况下,你是一个泡菜,因为上传的东西不是input,而是一个Flash对象.WebDriver没有允许您使用浏览器对话框(或Flash对象)的API.

因此,在您单击Flash元素后,会弹出一个窗口,您将无法控制.在我所知道的浏览器和操作系统中,您几乎可以假设在窗口打开后,光标位于File name输入中.请确保在您的情况下这个假设也是正确的.

如果没有,你可以尝试通过按下来跳转到它Alt + N,至少在Windows上...

如果是,您可以"盲目地"使用Robot该类输入路径.在你的情况下,这将是一种方式:

driver.findElement(By.id("SWFUpload_0")).click();
Robot r = new Robot();
r.keyPress(KeyEvent.VK_C);        // C
r.keyRelease(KeyEvent.VK_C);
r.keyPress(KeyEvent.VK_COLON);    // : (colon)
r.keyRelease(KeyEvent.VK_COLON);
r.keyPress(KeyEvent.VK_SLASH);    // / (slash)
r.keyRelease(KeyEvent.VK_SLASH);
// etc. for the whole file path

r.keyPress(KeyEvent.VK_ENTER);    // confirm by pressing Enter in the end
r.keyRelease(KeyEvent.VK_ENTER);
Run Code Online (Sandbox Code Playgroud)

它很糟糕,但它应该工作.请注意,您可能需要以下内容:如何让Robot键入`:`?并将String转换为KeyEvents(另外还有new和shiny KeyEvent#getExtendedKeyCodeForChar(),它们可以完成类似的工作,但只能从JDK7获得).


对于Flash,我所知道的唯一替代方案(来自此讨论)是使用暗技术:

首先,修改Flash应用程序的源代码,使用ActionScript的ExternalInterface API公开内部方法.一旦公开,这些方法将由浏览器中的JavaScript调用.

其次,现在JavaScript可以在您的Flash应用程序中调用内部方法,您可以使用WebDriver在网页中进行JavaScript调用,然后调用您的Flash应用程序.

这项技术在flash-selenium项目的文档中进一步解释.(http://code.google.com/p/flash-selenium/),但该技术背后的理念同样适用于WebDriver.

  • "不工作"是什么意思?为什么,在哪里,有什么错误信息? (2认同)

Emm*_*o.R 10

下面的代码工作对我来说:

public void test() {
    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.freepdfconvert.com/pdf-word");
    driver.findElement(By.id("clientUpload")).click();
    driver.switchTo()
            .activeElement()
            .sendKeys(
                    "/home/likewise-open/GLOBAL/123/Documents/filename.txt");
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    driver.findElement(By.id("convertButton"));
Run Code Online (Sandbox Code Playgroud)

  • 这应该是答案.sendKeys()根本不会产生任何结果,但这个完美无缺.我在Ubuntu上使用GhostDriver. (2认同)

Mat*_*att 5

Using C# and Selenium this code here works for me, NOTE you will want to use a parameter to swap out "localhost" in the FindWindow call for your particular server if it is not localhost and tracking which is the newest dialog open if there is more than one dialog hanging around, but this should get you started:

    using System.Threading;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using OpenQA.Selenium;

    [DllImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool SetForegroundWindow(IntPtr hWnd);

    [DllImport("user32.dll", EntryPoint = "FindWindow")]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    public static void UploadFile(this IWebDriver webDriver, string fileName)
    {
        webDriver.FindElement(By.Id("SWFUpload_0")).Click();
        var dialogHWnd = FindWindow(null, "Select file(s) to upload by localhost");
        var setFocus = SetForegroundWindow(dialogHWnd);
        if (setFocus)
        {
            Thread.Sleep(500);
            SendKeys.SendWait(fileName);
            SendKeys.SendWait("{ENTER}");
        }
    }
Run Code Online (Sandbox Code Playgroud)