【问题标题】:Selenium (Java) - Chrome Headless - Unable to upload the fileSelenium (Java) - Chrome Headless - 无法上传文件
【发布时间】:2020-04-16 10:13:07
【问题描述】:

需要一个与在无头模式下在 Selenium 脚本中上传文件逻辑相关的输入。 实现了机器人类来模拟键盘命令,但它只在非无头模式下工作。

上传按钮的外观如下:

单击上传简历会显示一个弹出窗口按钮。在无头模式下点击“上传简历”按钮后,没有弹出窗口(截图)。

上传按钮周围的页面来源:

<div class="resume_upload_block">
<div id="resumeFile" class="controls-dz dropzone dz-clickable registration_groupl resume_drag_drop">
<div class="dz-default dz-message">
<div class="fa fa-upload"></div>
<label for="file-upload" class="custom-file-upload" title="Upload Resume"> 
<input type="text" name="resume" id="resume" value="" placeholder="Browse *" style=""/>
</label>
</div>
<div class="clearfix">
</div>
</div>
</div>

研究了 AutoIT 和 Sikuli 工具等选项。没有一个选项提供有关如何在无头模式下运行的信息。

【问题讨论】:

  • 你试过webDriver.findElement(By.id("resume")).sendKeys("your/path/to/file")吗?
  • 是的@MinhDao,我也尝试过这个选项。 org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of [[ChromeDriver: chrome on XP (dc9bdf7867a80d2afa276eeedfe3c6d9)] -&gt; id: resume] (tried for 15 second(s) with 500 milliseconds interval)
  • 您似乎正在使用 WebDriverWait 来检查该输入是否可见。但您可能想在 DevTool 中检查(FireFox 或 Chrome 中的 F12)。我在想你的Upload resume 按钮实际上不是 HTML 输入,真正的输入是从视图中隐藏的,这就是你得到TimeoutException 的原因。你能提供你的链接让我看看吗?
  • @MinhDao: Click here 点击应用 --> 新用户 --> 你会看到上传按钮。
  • 我还没有解决,所以我需要更多的时间来投资。但我发现了这个:Upload file with selenium IDE to Dropzone JS。希望这会对你有所帮助。

标签: java selenium file-upload headless-browser


【解决方案1】:

对不起,我的回答迟了。经过一番搜索,我发现真正的文件输入不在上传按钮下。代码如下:

WebElement inputResume = webDriver.findElement(By.className("dz-hidden-input"));
inputResume.sendKeys("/full/path/to/your/file"); // For example: /home/minhdao/Downloads/resume.pdf

我还创建了一个gist。如果您想要一个无需配置即可执行的完整项目,请随时发表评论。

【讨论】:

  • 感谢您的回复。我尝试将 findby 与您提到的类名一起使用。获取 StaleElement 异常。 org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
  • 您可能会在某些使您的元素消失的事件发生之前找到该元素(这称为重新渲染 DOM)。我已经上传了一个带有完整代码的 git repo:TestUploadFile。在README.md 文件中有一个运行这个项目的简单指南。如果有任何问题,请随时在以下位置创建问题:Issues
猜你喜欢
  • 2018-07-15
  • 2019-05-25
  • 2017-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-30
  • 2018-05-24
  • 2018-06-08
相关资源
最近更新 更多