2013-02-23 47 views
2

互動我試圖附加文件使用水豚。 輸入元素是:附加文件與水豚 - 元素目前不可見,>因此可能不會與

<input type="file" multiple="multiple" name="uploadfile[]" style="position: absolute; margin: 0px 0px 0px -175px; padding: 0px; width: 220px; height: 30px; font-size: 14px; opacity: 0; cursor: pointer; display: block; z-index: 2147483583; top: 3331px; left: 436px;"> 

代碼附加文件是:

attach_file 'uploadfile[]', '/home/user/image.jpg' 

但它會返回錯誤:

[email protected]:~/Documents/Aptana Studio 3 Workspace/AddPlace$ ruby 
test.rb [remote server] 
file:///tmp/webdriver-profile20130223-30353-dw596v/extensions/[email protected]/components/command_processor.js:7537:in 
`fxdriver.preconditions.visible': Element is not currently visible and 
so may not be interacted with 
(Selenium::WebDriver::Error::ElementNotVisibleError) from [remote 
server] 
file:///tmp/webdriver-profile20130223-30353-dw596v/extensions/[email protected]/components/command_processor.js:10207:in 
`DelayedCommand.prototype.checkPreconditions_' from [remote server] 
file:///tmp/webdriver-profile20130223-30353-dw596v/extensions/[email protected]/components/command_processor.js:10226:in 
`DelayedCommand.prototype.executeInternal_/h' from [remote server] 
file:///tmp/webdriver-profile20130223-30353-dw596v/extensions/[email protected]/components/command_processor.js:10231:in 
`DelayedCommand.prototype.executeInternal_' from [remote server] 
file:///tmp/webdriver-profile20130223-30353-dw596v/extensions/[email protected]/components/command_processor.js:10171:in 
`DelayedCommand.prototype.execute/<' from 
/usr/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/remote/response.rb:52:in 
`assert_ok' from 
/usr/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/remote/response.rb:15:in 
`initialize' from 
/usr/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/remote/http/common.rb:59:in 
`new' from 
/usr/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/remote/http/common.rb:59:in 
`create_response' from 
/usr/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/remote/http/default.rb:66:in 
`request' from 
/usr/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/remote/http/common.rb:40:in 
`call' from 
/usr/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/remote/bridge.rb:615:in 
`raw_execute' from 
/usr/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/remote/bridge.rb:593:in 
`execute' from 
/usr/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/remote/bridge.rb:400:in 
`sendKeysToElement' from 
/usr/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/common/element.rb:126:in 
`send_keys' from 
/usr/lib/ruby/gems/1.9.1/gems/capybara-2.0.2/lib/capybara/selenium/node.rb:33:in 
`set' from 
/usr/lib/ruby/gems/1.9.1/gems/capybara-2.0.2/lib/capybara/node/element.rb:79:in 
`block in set' from 
/usr/lib/ruby/gems/1.9.1/gems/capybara-2.0.2/lib/capybara/node/base.rb:78:in 
`synchronize' from 
/usr/lib/ruby/gems/1.9.1/gems/capybara-2.0.2/lib/capybara/node/element.rb:79:in 
`set' from 
/usr/lib/ruby/gems/1.9.1/gems/capybara-2.0.2/lib/capybara/node/actions.rb:144:in 
`attach_file' from 
/usr/lib/ruby/gems/1.9.1/gems/capybara-2.0.2/lib/capybara/session.rb:341:in 
`block (2 levels) in <class:Session>' from 
/usr/lib/ruby/gems/1.9.1/gems/capybara-2.0.2/lib/capybara/dsl.rb:51:in 
`block (2 levels) in <module:DSL>' from test.rb:19:in `add_place' 
    from test.rb:11:in `run' from test.rb:33:in `<main>' 

如何解決這個問題?

+0

嗨伊萬,我不認爲你的代碼有什麼問題。可能該頁面尚未完全加載,並且您嘗試過早地抓住元素。理想情況下,您應該在嘗試附加之前引入延遲。 – 2013-02-28 15:31:49

回答

-3

因爲Ruby是不是很好的頁面加載,如果我是你,我會解決這個問題是這樣的:

!30.times { if (driver.find_element(:xpath, "//input[@type='file']")rescue false) then break else sleep 1; end } 

基本上上面的代碼將嘗試30次(每1秒)來搜索如果元素存在,則試圖附加的元素將退出循環。如果需要,可以在休息之後或睡眠命令之後使用變量來指導流程。

祝您好運

3
attach_file('locator', 'path/to/file.jpg', :visible => false) 

的應該做的伎倆。水豚默認忽略隱藏的領域,所有你需要做的就是通過一個可見性選項

0

:visible選項對我沒有任何幫助。這裏似乎是一個:make_visible選項:

attach_file 'uploadfile[]', '/home/user/image.jpg', :make_visible => true 

或代替true與將它設置試圖打開文件對話框之前覆蓋自定義樣式屬性的哈希值。

相關問題