2

我正在尝试使用 Capybara 附加文件。输入元素是:

<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'

但它返回错误:

biske@biske:~/Documents/Aptana Studio 3 Workspace/AddPlace$ ruby
test.rb  [remote server]
file:///tmp/webdriver-profile20130223-30353-dw596v/extensions/fxdriver@googlecode.com/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/fxdriver@googlecode.com/components/command_processor.js:10207:in
`DelayedCommand.prototype.checkPreconditions_'  from [remote server]
file:///tmp/webdriver-profile20130223-30353-dw596v/extensions/fxdriver@googlecode.com/components/command_processor.js:10226:in
`DelayedCommand.prototype.executeInternal_/h'   from [remote server]
file:///tmp/webdriver-profile20130223-30353-dw596v/extensions/fxdriver@googlecode.com/components/command_processor.js:10231:in
`DelayedCommand.prototype.executeInternal_'   from [remote server]
file:///tmp/webdriver-profile20130223-30353-dw596v/extensions/fxdriver@googlecode.com/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>'

如何解决这个问题?

4

3 回答 3

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

应该做的伎俩。Capybara 默认忽略隐藏字段,你需要做的就是传入一个可见性选项

于 2014-11-28T14:52:45.650 回答
1

:visible选项对我没有任何作用。似乎有一个:make_visible选项:

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

或者true使用自定义样式属性的哈希值,在尝试打开文件对话框之前将其设置为覆盖。

于 2018-03-05T09:53:59.350 回答
-4

因为Ruby对页面加载不是很好,如果我是你,我会这样解决:

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

基本上,上面的代码将尝试 30 次(每 1 秒)来搜索您尝试附加的元素,如果该元素存在,则将退出循环。如果需要,您可以在 break 之后或 sleep 命令之后使用变量来指导您的流程。

祝你好运

于 2013-02-28T15:37:33.880 回答