Selenium WebDriver 上传文件,输入类型为“text”

时间:2021-07-17 16:31:17

标签: python selenium selenium-webdriver selenium-chromedriver

我正在尝试将文件上传到表单中,但没有“文件”类型的输入元素。

我可以删除“readonly”属性并将键发送到输入文本框,但是当我单击上传按钮时会引发一般错误。 (只是一个没有有用信息的弹出窗口)

这是呈现的元素:

<input type="text" autocomplete="off" class="form-control animate-show 
ng-pristine ng-empty ng-invalid ng-invalid-required ng-touched" 
id="filepath" ng-model="model.name" ng-required="configuration.Required" 
readonly="" required="required" style="">

这是手动选择文件后的元素:

<input type="text" autocomplete="off" class="form-control animate-show 
ng-pristine ng-touched ng-not-empty ng-valid ng-valid-required" 
id="filepath" ng-model="model.name" ng-required="configuration.Required" 
readonly="" required="required" style="">

这是删除“只读”并通过硒发送我的文件路径后的元素:

<input type="text" autocomplete="off" class="form-control animate-show 
ng-touched ng-not-empty ng-dirty ng-valid-parse ng-valid ng-valid-required" 
id="filepath" ng-model="model.name" 
ng-required="configuration.Required" required="required" style="">

测试设置: Ubuntu 20 蟒蛇 3.8 铬 91.0.4472.114 硒 3.141.0

更新: 我在下面找到了“文件”类型的输入字段,但它位于页面底部。 (不在预期的模式内) 将此元素的路径传递成功(没有 selenium 错误),但上传过程仍然失败。

<label tabindex="-1" style="visibility: hidden; position: absolute; 
overflow: hidden; width: 0px; height: 0px; border: none; margin: 0px; 
padding: 0px;">upload<input type="file" ngf-select="" 
ng-model="model" ng-show="!model"></label> 
"upload" 
<input type="file" ngf-select="" ng-model="model" ng-show="!model"> 
<label tabindex="-1" style="visibility: hidden; position: absolute; 
overflow: hidden; width: 0px; height: 0px; border: none; margin: 0px; 
padding: 0px;">upload 
<input type="file" ngf-select="" ng-model="model" ng-show="!model">
</label>

1 个答案:

答案 0 :(得分:1)

事实证明在页面的不同部分有一个 type="file 的元素。访问正确的元素并使用 send_keys 和文件路径完美地工作。有问题的原始文本框填充了文件名和文件上传正确。

感谢大家的投入,特别是 @YaDavMaNish 的语法最终使这项工作发挥了作用。