【问题标题】:How to handle <p:fileUpload auto="true"> with Selenium?如何使用 Selenium 处理 <p:fileUpload auto="true">?
【发布时间】:2019-03-04 15:58:19
【问题描述】:

我正在使用 Primeface 的 (6.1.1) p:fileUpload 组件和 auto="true"。我还没有找到使用 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 键。

我真的被困在这里了。如何做到这一点?

提前致谢!

【问题讨论】:

    标签: java selenium file-upload primefaces


    【解决方案1】:

    @kopfarzt 你尝试过传统方式吗:

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

    【讨论】:

    • 我没有名为 ..._0 的元素。为了澄清,我将生成的 HTML 代码添加到我的问题中。
    • 您有一个输入标签,它将接受文件位置。只需将您的 html 的 id 替换为我提供的示例即可。例如:By.id("myContainer:myUpload_input")
    • 哦,你是对的,它有效!显然,“新”方式(使用动作)的行为与“传统”方式不同。非常感谢!
    【解决方案2】:

    试试下面的代码

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

    【讨论】:

    • 我没有名为 ..._0 的元素。为了澄清,我将生成的 HTML 代码添加到我的问题中。
    • 投票支持该解决方案,因为它实际上与@Infern0 的解决方案相同。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2013-11-28
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 2021-07-20
    相关资源
    最近更新 更多