首页 > 解决方案 > 如何使用 xpath 作为 sendkeys 从 selenium 上传只读文本文件将不起作用

问题描述

这是结构:

<input _ngcontent-c9="" class="form-field" placeholder="Upload Emote" readonly="" type="text">

我已使用以下代码上传文件,但 sendkeys 不起作用,我没有收到任何错误但无法上传。

    WebElement uploadingEmote = adminDriver.findElement(By.xpath("//input[@placeholder='Upload Emote']"));
    uploadingEmote.sendKeys("/home/smart/Downloads/EMO/(Heart).png");

但是上面的代码不起作用,因为该字段是只读的。任何帮助,将不胜感激。

这是屏幕截图:

标签: javahtmlangularjsseleniumfile-upload

解决方案


<input _ngcontent-c9="" class="form-field" placeholder="Upload Emote" readonly="" type="text">

如果要上传文件,您的输入字段类型应为“文件”类型

<input type="file" name="fileToUpload" id="fileToUpload">

推荐阅读