简体   繁体   中英

How to handle <p:fileUpload auto=“true”> with Selenium?

I am using Primeface's (6.1.1) p:fileUpload component with auto="true". I did not yet find a solution to upload a file with Selenium (3.14.0).

The xhtml code looks like this:

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

The generated html code looks like this:

<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>

I found solutions for auto="false" but not for auto="true". I tried to send the file path to the input element:

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

But this was obviously not enough, even when appending the RETURN key.

I am really stuck here. How can this be done?

Thanks in advance!

@kopfarzt did you try the traditional way:

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

Try below code

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");

        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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