【问题标题】:Selenium WebDriver Upload File With Input Type of "text"Selenium WebDriver 上传文件,输入类型为“文本”
【发布时间】:2021-09-26 01:08:35
【问题描述】:

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

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

这是呈现的元素:

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

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

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

【问题讨论】:

  • 我不认为你可以在type="text"输入中上传文件,我认为你应该上传文件路径!有 id 表示 filepath
  • 您是否尝试通过发送键将文件路径作为输入传递?一般错误是什么意思?你能分享弹出窗口或页面网址吗?
  • 你需要检查整个源html,如果任何输入标签存在类型为文件,你可以做send_keys(),也许你也可以检查这个css,input[type='file']
  • 你可以试试这个 Xpath ((//input[@type='file'])[1]) 我可以看到你有 3 个基于索引选择的输入标签
  • @YaDavMaNish - 它不适用于索引 1,但它确实适用于 2。看起来这会起作用!非常感谢!

标签: python selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

原来在页面的不同部分有一个带有 type="file 的元素。访问正确的元素并使用带有文件路径的 send_keys 效果很好。有问题的原始文本框填充了文件名和文件上传正确。

感谢大家的意见,特别是 @YaDavMaNish 的语法,最终使它工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    相关资源
    最近更新 更多