我尝试在我的 Instagram 上上传图片,所以我需要选择上传文件的路径
但我不能使用 form.file_field.send_keys(path)
因为 Instagram 通过 JS 管理上传,所以表单不存在,只有当我点击按钮“+”时才会出现“文件上传”窗口。
我试试:
@browser.send_keys @path
@browser.send_keys :enter
但不是太有效...
我没有找到与此子窗口“文件上传”交互以提供图像路径的方法。
有什么想法吗?
编辑:
<nav class="NXc7H f11OC "><div class="_8MQSO ZoygQ "><div class=""><div class="rBWT5"></div><div class="KGiwt"><div class="A8wCM"><div class="BvyAW"><div class="q02Nz"><a class="_0TPg" href="/"><span class="glyphsSpriteHome__outline__24__grey_9 u-__7" aria-label="Home"></span></a></div><div class="q02Nz"><a class="_0TPg" href="/explore/"><span class="glyphsSpriteSearch__outline__24__grey_9 u-__7" aria-label="Search & Explore"></span></a></div><div class="q02Nz _0TPg" role="menuitem" tabindex="0"><span class="glyphsSpriteNew_post__outline__24__grey_9 u-__7" aria-label="New Post" style=""></span></div><div class="q02Nz"><a class="_0TPg " href="/accounts/activity/"><span class="glyphsSpriteHeart__outline__24__grey_9 u-__7" aria-label="Activity"></span></a></div><div class="q02Nz"><a class="_0TPg" href="/tristan_grey_30/"><span class="glyphsSpriteUser__filled__24__grey_9 u-__7" aria-label="Profile"></span></a></div></div></div></div><form class="Q9en_" enctype="multipart/form-data" method="POST" role="presentation"><input accept="image/jpeg" class="tb_sK" type="file"></form></div></div></nav>
如果我尝试使用 <form>
包含在 <nav>
中,没有任何反应,“+”上有 onClick 事件:
{
!0 !== this.$_MobileNav2 && (this.$_MobileNav2 = !0, r(d[1]).logAction_DEPRECATED('cameraIconClick'), this.$_MobileNav3 ? (this.$_MobileNav3.selectFile(), this.props.onStartCreation()) : (i(d[2])('No image form'), this.props.onImageFormError()), this.$_MobileNav2 = !1)
}
我认为它是由 JS 管理的...
最佳答案
您不会喜欢这个答案,但是 Watir
将不会以任何方式与操作系统打开的窗口进行交互。更进一步,Ruby 本身不与这些操作系统级别的对话进行交互。
您可能需要引用几件事来确认这一点,这是完全可以接受的:
- Watir's Philosophy on Downloads (and conversely Uploading)
- An answer by Thomas Walpole explaining what I just did
- A possible solution using
Capybara::Node::Actions#attach_file
在任何情况下,您都试图与一个无法通过 Ruby 进行交互的对象进行交互,更不用说 Watir 了,因此您想要的解决方案是不可能的。
最好的情况是 Capybara attach_file
方法。如果这不起作用,则除了 AutoIt
或 Sikuli
您可以找到 SikuliX 项目页面 here . DrapsTV 做了一个大约一个小时的播放列表,内容是关于 SikuliX 的设置和快速启动。该系列第一集的链接已链接here .
祝你好运。
关于javascript - watir ruby 文件上传 windows 交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55924938/