没有选择文件按钮时上传图像

时间:2019-09-08 15:21:35

标签: selenium-webdriver automated-tests

In This image there no choose file button manually we can drag and drop files from system but I want to automate this functionality我必须在框上执行拖放操作,但是框上没有任何按钮可以单击,这是我必须通过系统放置图像的唯一区域。

我尝试使用AutoIt,Robotclass,ActionClass,但没有任何效果。

//using Robot class
public static void uploadFile(String fileLocation) throws Exception {

                //Setting clipboard with file location
                setClipboardData(fileLocation);
                //native key strokes for CTRL, V and ENTER keys
                Robot robot = new Robot();

                robot.keyPress(KeyEvent.VK_CONTROL);
                robot.keyPress(KeyEvent.VK_V);
                robot.keyRelease(KeyEvent.VK_V);
                robot.keyRelease(KeyEvent.VK_CONTROL);
                robot.keyPress(KeyEvent.VK_ENTER);
                robot.keyRelease(KeyEvent.VK_ENTER);
    }

//Using below code
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));
        }

我收到错误消息,因为元素不可单击,因为没有按钮来选择文件,而html代码只有类。

0 个答案:

没有答案