如何使用Selenium处理<p:fileupload auto =“ true”>?

时间:2018-09-28 15:59:37

标签: java selenium file-upload primefaces

我正在使用带有auto =“ true”的Primeface(6.1.1)p:fileUpload组件。我尚未找到使用Selenium(3.14.0)上传文件的解决方案。

xhtml代码如下:

<p:fileUpload id="myUpload" mode="advanced" auto="true"...>

生成的html代码如下:

<div id="myContainer:myUpload" class="ui-fileupload ui-widget ui-fileupload-responsive">
    <div class="ui-fileupload-buttonbar ui-widget-header ui-corner-top">
        <span class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left ui-fileupload-choose" tabindex="0" role="button" aria-labelledby="myContainer:myUpload_label">
            <span class="ui-button-icon-left ui-icon ui-c ui-icon-plusthick"/>
            <span id="myContainer:myUpload_label" class="ui-button-text ui-c">Select File</span>
            <input id="myContainer:myUpload_input" name="myContainer:myUpload_input" tabindex="-1" type="file">
        </span>
    </div>
    <div class="ui-fileupload-content ui-widget-content ui-corner-bottom">
        <div class="ui-messages ui-widget ui-helper-hidden ui-fileupload-messages">
            <div class="ui-messages-error ui-corner-all">
                <a class="ui-messages-close" href="#">
                    <span class="ui-icon ui-icon-close"/>
                </a>
                <span class="ui-messages-error-icon"/>
                <ul/>
            </div>
        </div>
        <div class="ui-fileupload-files">
            <div/>
        </div>
    </div>
</div>

我找到了针对auto =“ false”而不是auto =“ true”的解决方案。我试图将文件路径发送到输入元素:

WebElement element = driver.findElement(By.id("...myUpload_input"));
new Actions(driver).sendKeys(element, mypath).perform();

但是,即使添加RETURN键,这显然还不够。

我真的被困在这里。该怎么办?

谢谢!

2 个答案:

答案 0 :(得分:3)

@kopfarzt您是否尝试过传统方式:

WebElement uploadElement = driver.findElement(By.id("uploadfile_0"));
uploadElement.sendKeys("C:\\newhtml.html");

答案 1 :(得分:1)

尝试下面的代码

public class Test{
    public static void main(String[] args) {
        System.setProperty("webdriver.firefox.marionette","C:\\geckodriver.exe");
        String baseUrl = "url";
        WebDriver driver = new FirefoxDriver();

        driver.get(baseUrl);
        WebElement uploadElement = driver.findElement(By.id("uploadfile_0"));

        // enter the file path onto the file-selection input field
        uploadElement.sendKeys("C:\\newhtml.html");

        }
}