【问题标题】:How to upload a file in Selenium with no text box如何在没有文本框的情况下在 Selenium 中上传文件
【发布时间】:2013-01-13 14:39:18
【问题描述】:

我一直在寻找在 Selenium 2 中上传文件的解决方案。

问题是我尝试上传的网络元素可以通过两种方式使用: 拖放,或单击按钮。没有字段输入框。并不是说我没有尝试过使用 sendKeys。我已经在按钮和所有周围元素上进行了尝试。

这个问题的第二部分是我在 Windows 机器上编写,但自动化发生在 Linux 机器上。这意味着 AutoIt 将不起作用。这是上传框的 HTML。

<div class="up-target" id="up-drop-zone">
    <div class="up-drop-zone-pre hidden">
        <p>Please choose a folder to upload</p>
    </div>
    <div class="up-drop-zone-decor">
        <p>Drop one or more files here</p>
        <p>or</p>
        <button name="uploadFile" class="upload">Select Files</button>
        <input type="file" id="up-drop-zone-input" name="files[]" multiple="true">
    </div>
</div>

我正在使用 Java,并且对 Selenium 之外的其他方法开放(但是,我只有选择的 maven 存储库)。

谢谢!

【问题讨论】:

    标签: java file-upload selenium automated-tests selenium-webdriver


    【解决方案1】:

    我发现让它工作的唯一方法是使用AutoIt(感谢LittlePandauser3903359 的回答)。

    我对脚本进行了改进,因为我发现在测试运行时执行任何其他操作都可能导致它停止工作。诀窍是找到窗口,然后在输入文本之前将其激活。

    超时是为了防止多个 AutoIt 脚本在后台挂起,这意味着当您停止测试并尝试做自己的工作时,它们会开始并尝试开始输入!

    请注意,窗口在不同浏览器中的名称不同(例如,Chrome 中的“打开”)。

    $windowHandle = WinWait("Choose File to Upload", "", 3) ; 3 second timeout - NB the window name will be different in different browsers!
    
    If $windowHandle == 0 Then
       MsgBox(0, "", "Upload popup not found")
    Else
       ;MsgBox(0, "", "Upload popup found: " & $windowHandle)
       WinActivate($windowHandle)
       Send("C:\\path\to\myfile.txt")
       Send("{ENTER}")
    EndIf
    

    根据所有其他答案,我假设从 Java 运行 AutoIt 脚本:

    Runtime.getRuntime().exec("MyAutoItScript.exe");
    

    从 C# 运行 AutoIt 脚本:

    var process = Process.Start(@"C:\\path\to\myAutoItScript.exe");
    process.WaitForExit();
    Thread.Sleep(200); // IE fix for Modal dialog present exception
    

    【讨论】:

      【解决方案2】:

      很遗憾,到目前为止(2013 年 1 月,Selenium 2.29.1)您还不能这样做,因为 Selenium 不支持 &lt;input type="file" multiple&gt; 元素。

      There is a feature enhancement request 这个是项目开发者自己做的,只是还没有实现。您可以在此处加注星标以将其在优先级列表中向上移动。

      另外,据我所知,您不能真正以可靠的方式将文件从桌面拖到WebElement

      解决方法可能是使用 AutoIT(仅限 Windows)或 Robot 类(也仅适用于与您的设置类似的设置)并“盲目地”在对话框中键入路径:

      driver.findElement(By.id("up-drop-zone-input")).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);
      

      这很糟糕,但它应该可以工作。请注意,您可能需要这些:How can I make Robot type a `:`?Convert String to KeyEvents(另外还有新的闪亮的 KeyEvent#getExtendedKeyCodeForChar(),它可以做类似的工作,但只能从 JDK7 获得)。

      【讨论】:

      • 那行得通。我可以获取我所在机器的本地文件路径,因此 Convert String to KeyEvents 链接非常有用。谢谢!
      【解决方案3】:

      我认为 autoIT 会解决这个问题 只是我测试中java代码的一部分

      String[] commands = new String[]{};
      commands = new String[]{"c:/test/attachFile.exe"};
      Runtime.getRuntime().exec(commands);
      

      【讨论】:

        【解决方案4】:

        这样试试

        driver.findElement(By.id("up-drop-zone-input")).sendKeys("filePath");
        

        【讨论】:

        • 我已经试过了。 sendKeys(在任何这些元素上)不起作用。它只是一个不接受文本的按钮。
        • 那其实不是按钮,文件类型输入标签。它应该与 sendKeys 一起使用。
        • 我无法让这个工作。它通常在硒中起作用,但在我的具体情况下,它没有。
        猜你喜欢
        • 2018-11-23
        • 2019-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-27
        • 2013-08-26
        • 1970-01-01
        相关资源
        最近更新 更多