Watir 不会选择我需要上传的文件

发布于 2024-11-26 12:00:15 字数 410 浏览 0 评论 0原文

我的 watir 脚本的一部分测试上传表单。我可以打开上传框,但它不会导航到文件夹并选择我想要的文件。相反,该脚本将崩溃并给出以下错误: 没有这样的文件或目录 - /Users/Abdulla/Documents/Ads/1.jpg (Errno::ENOENT)

这是我的代码,我不确定我是否做对了。顺便说一句,这是在 mac 上使用 chromewatir。

browser.file_field(:name, "u").click
browser.file_field(:name, "u").set("/Users/Abdulla/Documents/Ads/1.jpg")

路径名是否应该以 /Users 开头?或者应该是/Documents?因为当它单击网站上的“选择文件”时,它会直接在“文档”文件夹中打开。

Part of my watir script tests an upload form. I can get the upload box to open, but it won't navigate to the folder and choose the file I want. Instead, the script will crash and give this error:
No such file or directory - /Users/Abdulla/Documents/Ads/1.jpg (Errno::ENOENT)

Here's my code, I'm not sure if I'm doing this right. This is using chromewatir on a mac btw.

browser.file_field(:name, "u").click
browser.file_field(:name, "u").set("/Users/Abdulla/Documents/Ads/1.jpg")

Is the path name supposed to start with /Users? Or should it be /Documents? Because when it clicks choose file on the site it opens right in the Documents folder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

铁轨上的流浪者 2024-12-03 12:00:15

1) 我建议切换到 Watir-Webdriver 来驱动 chrome。正如评论中所指出的,chrome-watir 项目此时几乎已经死亡

2) 检查文件是否存在。

3) 检查运行 watir(或 watir-webdriver)的用户是否有权访问该文件。或者考虑将文件放在更公共的目录中(就目录的权限而言)

4)我不能肯定在Mac上,但在PC上,chrome上传文件选择器会记住用于上传文件的最后一个目录,并且打开到相同的位置(大概是为了方便用户),但是指定文件的完整文件路径应该不是问题。如果您需要确定的话,您可以随时尝试手动执行此操作,看看它是否有效。

5)我不确定我们能否告诉您正确的路径是什么,毕竟涉及到您的系统、您的用户和您的文件,您比任何人都更了解您在这方面所做的事情我们会的。

1) I would recommend switching to Watir-Webdriver to drive chrome. As indicated in the comments, the chrome-watir project is pretty much dead at this point

2) check that the file exists.

3) check that the user which watir (or watir-webdriver) is running under has access to that file. or consider putting the file in a more public directory (in terms of permissions to the directory)

4) I can't say for sure on mac, but on a PC, the chrome upload file selector remembers the last directory used to upload files and opens to the same place (presumably as a convenience to the user), however specifying the full file path to the file should not be a problem. You could always try doing that manually and see if it works or not if you need to be sure.

5) I'm not sure we can tell you what the correct path is, after all it's your system, your users, and your file that are involved, you'd know far more about what you are doing in that respect than any of us would.

苏别ゝ 2024-12-03 12:00:15

我的猜测是 /Users/Abdulla/Documents/Ads/1.jpg 文件不存在,如错误消息所示。

My guess is that /Users/Abdulla/Documents/Ads/1.jpg file does not exist, as the error message says.

愿与i 2024-12-03 12:00:15

如果您使用“watir-webdriver”,则可以使用此代码。

我认为你不需要点击文件字段,只需要设置文件路径。

local_file = "/Users/Abdulla/Documents/Ads/1.jpg"
puts (File.exists? local_file)
browser.file_field(:id => 'upComponent').set  local_file

You can use this code if you use 'watir-webdriver'.

I think you don't need click the file field, only need to set the file path.

local_file = "/Users/Abdulla/Documents/Ads/1.jpg"
puts (File.exists? local_file)
browser.file_field(:id => 'upComponent').set  local_file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文