简体   繁体   English

使用Selenium HtmlUnitDriver-headless webdriver上传文件

[英]File Upload using Selenium HtmlUnitDriver-headless webdriver

I'm trying to upload a local file (C:\\sample.txt) to my server. 我正在尝试将本地文件(C:\\ sample.txt)上传到我的服务器。 I have tried to implement this with Chrome web driver and its working absolutely fine. 我尝试使用Chrome网络驱动程序来实现这一点,并且它的工作原理绝对不错。 But during implementing the same with HTMLUnitDriver, i couldn't browse the file item from my local disk. 但是在用HTMLUnitDriver实现相同功能的过程中,我无法从本地磁盘浏览文件项。 I tried the below two methods as well, 我也尝试了以下两种方法,

1) Send keys: 1)发送密钥:

        WebElement inputFile = driver.findElement(By.id("file"));
        System.out.println(driver.getCurrentUrl());
        LocalFileDetector detector = new LocalFileDetector();
        String path = "C:\\UploadSample1.txt";
        File f = detector.getLocalFile(path);
        inputFile.sendKeys(f.getAbsolutePath());

2) Using a Robot: 2)使用机器人:

        WebElement browseFile = fluentWait(By.id("browseFile"), driver);
        browseFile.click();
        File file = new File("C:\\UploadSample1.txt");
        driver.switchTo().activeElement();
        StringSelection fileNameToWrite = new StringSelection(
                file.getAbsolutePath());
        Toolkit.getDefaultToolkit().getSystemClipboard()
                .setContents(fileNameToWrite, null);
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
        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);

I need the file item to be browsed, then only i can save it to my server. 我需要浏览文件项,然后只有我才能将其保存到服务器。 Because just sending the file path will be searching the file in server disk. 因为仅发送文件路径将在服务器磁盘中搜索文件。 Now i'm really stuck and couldn't move further. 现在我真的被卡住了,无法继续前进。

Any help is highly appreciated. 非常感谢您的帮助。 Thankyou! 谢谢!

If you need to browse to the file first, that isn't possible IMHO; 如果您需要先浏览到文件,恕我直言,这是不可能的; for that you will need AutoIT (as Robot class is not recommended). 为此,您将需要AutoIT(不建议使用Robot类)。 So your best bet would be sending file path using sendKeys. 因此,最好的选择是使用sendKeys发送文件路径。

formInput.setValueAttribute(formValue); worked fine for me. 对我来说很好。

Code snippet: 程式码片段:

Iterator<String> formValueIterator =  formValues.keySet().iterator();
while(formValueIterator.hasNext()){
    String formKey = formValueIterator.next();
    String formValue = formValues.get(formKey);

    HtmlInput formInput =  form.getInputByName(formKey);

    if (formInput != null)
        if (formInput instanceof HtmlPasswordInput) {
            ((HtmlPasswordInput)formInput).setValueAttribute(formValue);
        } else {
            formInput.setValueAttribute(formValue);
        }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM