首页 > 解决方案 > 在 Selenium 中导入文件

问题描述

页面包含以下内容form

<form target="PID297_TGT_FRAME" action="/app/upload/57aa897a64d9" class="form form-default" method="post" enctype="multipart/form-data">
    <div>
        <input type="hidden">
        <input name="PID297_file" class="file-upload" type="file">
        <div aria-pressed="false" role="button" class="v-button" tabindex="0">
            <span class="v-button-wrap">
                <span class="v-button-caption">Import</span>
            </span>
        </div>
    </div>
</form>

现在我想将文件上传到表单。在搜索 stackoverflow 后,我发现可以input使用 type发送文件路径file。所以我这样做了:

var elem = Driver.FindElement(By.Name("PID297_file")).SendKeys(filePath);

不幸的是,我收到Exception了消息:

Element is not currently visible and so may not be interacted with

我的代码有问题吗?

标签: c#seleniumselenium-webdriverxpathcss-selectors

解决方案


该元素需要对 selenium 可见才能访问它。如果有其他动作可以启用文件上传,请先在 selenium 代码中执行。


推荐阅读