Watir-Webdriver:在 Firefox 上上传文件无法正常工作

发布于 2024-12-02 12:24:17 字数 517 浏览 1 评论 0原文

使用此代码我无法将文件正确上传到网站。

browser.form(:index, 2).file_field(:name, "filedata").set(""+folderName+"/iTunesArtwork")

代码运行后,它显示正在通过显示旋转轮进行上传,但它实际上从未上传,并且轮子只是不断旋转。如果我自己上传的话,虽然上传得很好。

这是 html 的链接: http://f.cl.ly/items/3v3o1p1g0t2S1q3q3Q1h/Text%202011.09.03%2011:40:06%20PM.html

如您所见,html位于表单标签中。如果我尝试在不先通过表单的情况下访问 file_field ,则会出现错误,指出元素无法交互,因为它不可见。有人知道发生了什么事吗?

Using this code I cannot get a file to upload correctly to the website.

browser.form(:index, 2).file_field(:name, "filedata").set(""+folderName+"/iTunesArtwork")

Once the code is run it shows that it's uploading by displaying a spinning wheel but it never actually uploads and the wheel just keeps spinning. If I upload it on my own though it uploads fine.

Here's a link for the html:
http://f.cl.ly/items/3v3o1p1g0t2S1q3q3Q1h/Text%202011.09.03%2011:40:06%20PM.html

As you can see the html is in a form tag. If I try to access the file_field without going through the form first it will give me an error saying element can't be interacted with because it's not visible. Anyone have a clue what's going on?

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

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

发布评论

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

评论(3

北渚 2024-12-09 12:24:17

您上传的本地文件不得在本地存在。

我已将您的 HTML 上传到:http://dl.dropbox.com/u/18859962/ uploader.html

我尝试了以下操作:

ruby-1.9.2-p290 :001 > require "watir-webdriver"
 => true 
ruby-1.9.2-p290 :002 > b = Watir::Browser.start "http://dl.dropbox.com/u/18859962/uploader.html"
 => #<Watir::Browser:0x..fdea53ebfe3940b9a url="http://dl.dropbox.com/u/18859962/uploader.html" title="untitled"> 
ruby-1.9.2-p290 :003 > b.file_field.exists?
 => true 
ruby-1.9.2-p290 :004 > local_file = "/users/me/ie.html"
 => "/users/me/ie.html" 
ruby-1.9.2-p290 :005 > File.exists? local_file
 => true 
ruby-1.9.2-p290 :006 > raise "error" unless File.exists? local_file
 => nil 
ruby-1.9.2-p290 :007 > b.file_field.set local_file
 => "/users/me/ie.html

请针对此托管 html 文件运行您的脚本并返回报告。

另外,如果本地文件不存在,您应该引发异常,以便消除该错误。

Your local file you are uploading mustn't exist locally.

I have uploaded your HTML to: http://dl.dropbox.com/u/18859962/uploader.html

I tried this:

ruby-1.9.2-p290 :001 > require "watir-webdriver"
 => true 
ruby-1.9.2-p290 :002 > b = Watir::Browser.start "http://dl.dropbox.com/u/18859962/uploader.html"
 => #<Watir::Browser:0x..fdea53ebfe3940b9a url="http://dl.dropbox.com/u/18859962/uploader.html" title="untitled"> 
ruby-1.9.2-p290 :003 > b.file_field.exists?
 => true 
ruby-1.9.2-p290 :004 > local_file = "/users/me/ie.html"
 => "/users/me/ie.html" 
ruby-1.9.2-p290 :005 > File.exists? local_file
 => true 
ruby-1.9.2-p290 :006 > raise "error" unless File.exists? local_file
 => nil 
ruby-1.9.2-p290 :007 > b.file_field.set local_file
 => "/users/me/ie.html

Please run your script against this hosted html file and report back.

Also, you should raise an exception if the local file doesn't exist, so that eliminates that error.

别念他 2024-12-09 12:24:17

在我开始为其提供文件的绝对路径之前,我无法让 file_field.set 工作。

我的代码看起来像这样:

relative_path = 'image.png'
full_path = File.expand_path relative_path
browser.file_field(:id, 'file_field_id').set full_path

希望这有帮助!

I wasn't able to get file_field.set to work until I started giving it an absolute path to the file.

My code looks something like:

relative_path = 'image.png'
full_path = File.expand_path relative_path
browser.file_field(:id, 'file_field_id').set full_path

Hope this helps!

沉睡月亮 2024-12-09 12:24:17

就你问题的这一部分而言:

如您所见,html 位于表单标记中。如果我尝试访问
file_field 无需先通过表格,它会给我一个
错误提示元素无法交互,因为它不是
可见的。有人知道发生了什么事吗?

最可能的答案是,DOM 中可能存在另一个具有相同名称且当前对用户不可见的输入字段。由于无法查看整个页面并查看所有 HTML,我不能肯定地说,但当我尝试与具有相同名称或文本的控件进行交互时,该错误是典型的,但目前隐藏在视图中。在页面代码中搜索我正在使用的名称或文本通常会发现其他元素。改变我处理问题的方式(通过指定一个唯一的容器,或者添加 :index 值以及现有标识符,然后将允许您获取元素的“正确”实例 。

您可以改用 :id 来标识字段(它有一个),然后看看这是否有效,而无需先指定外部(表单)容器元素,通常首选按 ID 选择 因为如果该值有效,则该值在页面上应该是唯一的然而,

这并没有解决您问题的“为什么不上传”方面,我怀疑这是您需要帮助的真正问题,但它确实解决了您问题的这一方面

,但没有 。由于我自己能够与该网站进行交互,所以很难说为什么它会这样(如果该网站是 itunes,您应该检查他们的服务条款中关于使用自动化访问该网站的内容)。

In terms of this part of your question:

As you can see the html is in a form tag. If I try to access the
file_field without going through the form first it will give me an
error saying element can't be interacted with because it's not
visible. Anyone have a clue what's going on?

The most likely answer is that there may be another input field in the DOM that has the same name and is not currently visible to the user. Without being able to look at the entire page and see all the HTML, I can't say for sure, but that error is typical of when I've tried to interact with controls that had the same name or text as others, but were currently hidden from view. searching the page code for the name or text I was using would usually discover the other element. Changing how I was addressing things (either by specifying a unique container as you are doing, or adding an :index value along with the existing identifier will then allow you to get the 'right' instance of the element.

You could switch to using :id to identify the field (it has one), and see if that works without having to specify the outer (form) container element first. Selecting by ID is generally preferred anyway as the value should be unique on the page if it's valid HTML.

That doesn't address the 'why doesn't it upload' aspect of your question however, which I suspect is the real problem you need help with. but it does address that aspect of your question.

As to the rest, without being able to interact with the site myself, it's difficult to say why it is behaving the way it is. (and if the site is itunes, you should check what their terms of service has to say about using automation to access the site)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文