-2

我正在测试一个使用文件选择器进行文件上传的应用程序:

https://www.inkfilepicker.com

我正在使用 watir。

我努力了:

def upload_photos
  $b.link(:text, "Upload Photos").click
  $b.button(:text, "Choose File").click
end

但代码失败:

`assert_exists': unable to locate element, using {:text=>"Choose File", :tag_name=>"button"} (Watir::Exception::UnknownObjectException

是否可以使用 watir 自动上传文件选择器?如何?

4

1 回答 1

0

编码

$b.button(:text, "Choose File").click

有两个问题(假设您的文件选择器与 inkfilepicker 演示页面上的相同):

  1. “选择文件”按钮位于 iframe 中。当涉及到框架时,您需要明确地告诉 Watir 。
  2. 选择文件不是常规按钮;它是文件字段 () 的按钮。这些是使用file_field方法从 Watir 访问的。不支持仅单击按钮。相反,有一种set方法可以单击按钮,选择要上传的文件并关闭窗口。

假设您的应用程序中的文件选择器与 inkfilepicker 演示页面上的文件选择器相同,您可以执行以下操作:

require 'watir-webdriver'
browser = Watir::Browser.new :firefox

# File to upload
file = 'C:\Users\user\Desktop\stuff.jpeg'

# Go to the demo page, which has a file uploader
browser.goto 'https://www.inkfilepicker.com/demos/'

# Click the button that opens the file uploader
browser.button(:class => 'zip-open-button').click

# Wait for the dialog to be displayed
browser.div(:id => 'filepicker_dialog_container').wait_until_present

# Set the file
browser.frame(:id => 'filepicker_dialog').file_field(:id => 'fileUploadInput').set(file)
于 2013-08-29T19:47:41.157 回答