Watir-Webdriver: Uploading files isn't working correctly on firefox
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 go开发者_运维百科ing 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?
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.
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!
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)
精彩评论