java - 我如何在表单 selenium 上上传文件

标签 java selenium

    <form method="post" enctype="multipart/form-data" class="box dropzone dz-clickable" action="/upload" id="drop">
     <div class="disable-click" style="display: none;">
     <a class="cross" href="javascript:void(0)">
     <img style="cursor:pointer;" src="http://demopayrollplus.finpay.pk/public/assets/images/cross-icon.png" alt="Remove File" title="Remove File"></a>
      </div>
      <div class="box__input dz-message">
           <img id="uploadIcon" src="http://demopayrollplus.finpay.pk/public/assets/images/upload-icon.svg">
           <label class="uploadedFileName">Drop your file here</label>
      </div>
 </form>

我尝试过以下方法

  1. 网络元素。
  2. 行动
  3. 发送 key

但没有任何作用,它说不是棘手的元素

WebElement ele = driver.findElement(By.id("drop"));
ele.sendkeys("file path");

最佳答案

对于拖放文件上传类型,您可以使用下面提到的代码。此代码将从您提到的位置拖动文件并将其放到文件上传位置。

ChromeDriver driver = new ChromeDriver();

driver.get("URL");

// locate the drop area
WebElement droparea = driver.findElement("file upload element");
Point point = droparea.getLocation();
        int xcord = point.getX();
        int ycord = point.getY();
// drop the file
DropFile(new File("location of file to be uploaded"), droparea, xcord ,ycord);


public static void DropFile(File filePath, WebElement target, int offsetX, int offsetY) {
        if(!filePath.exists())
            throw new WebDriverException("File not found: " + filePath.toString());

        WebDriver driver = ((RemoteWebElement)target).getWrappedDriver();
        JavascriptExecutor jse = (JavascriptExecutor)driver;
        WebDriverWait wait = new WebDriverWait(driver, 30);

        String JS_DROP_FILE =
            "var target = arguments[0]," +
            "    offsetX = arguments[1]," +
            "    offsetY = arguments[2]," +
            "    document = target.ownerDocument || document," +
            "    window = document.defaultView || window;" +
            "" +
            "var input = document.createElement('INPUT');" +
            "input.type = 'file';" +
            "input.style.display = 'none';" +
            "input.onchange = function () {" +
            "  var rect = target.getBoundingClientRect()," +
            "      x = rect.left + (offsetX || (rect.width >> 1))," +
            "      y = rect.top + (offsetY || (rect.height >> 1))," +
            "      dataTransfer = { files: this.files };" +
            "" +
            "  ['dragenter', 'dragover', 'drop'].forEach(function (name) {" +
            "    var evt = document.createEvent('MouseEvent');" +
            "    evt.initMouseEvent(name, !0, !0, window, 0, 0, 0, x, y, !1, !1, !1, !1, 0, null);" +
            "    evt.dataTransfer = dataTransfer;" +
            "    target.dispatchEvent(evt);" +
            "  });" +
            "" +
            "  setTimeout(function () { document.body.removeChild(input); }, 25);" +
            "};" +
            "document.body.appendChild(input);" +
            "return input;";

        WebElement input =  (WebElement)jse.executeScript(JS_DROP_FILE, target, offsetX, offsetY);
        input.sendKeys(filePath.getAbsoluteFile().toString());
        wait.until(ExpectedConditions.stalenessOf(input));
    }

关于java - 我如何在表单 selenium 上上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55487811/

相关文章:

java - 按下按钮时绘制字符串、矩形或椭圆形的 GUI

java - Collat​​or 无法在我的 Mac 上运行

javascript - Protractor 中的 `browser.call()` 代表什么?

java - ChromeDriver 启动 Chrome.app,但不驱动任何东西。我收到 : Could not start a new session

java - 如何在Spring Security中定义模式?

java - 我如何知道 HttpServletRequest 是否受 <security-constraint> 约束?

java - 如何在 Android 中使用 intentservice 同时下载多个文件?

javascript - 如何使用 Javascript :void(0) Selenium? 单击按钮

Python - 使用 Selenium 单击滚动到底部

java - 我在 expedia.com 网站上使用 pagefactory 找不到使用 id 或 xpath 的按钮