我正在测试一个使用文件选择器上传文件的应用程序:
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自动上传文件选择器?怎样
代码
$b.button(:text, "Choose File").click
有两个问题(假设您的文件选择器与inkfilepicker演示页面上的文件选择器相同):
- "选择文件"按钮位于iframe中。当涉及到框架时,您需要明确地告诉Watir它们
- "选择文件"不是常规按钮;它是一个文件字段()的按钮。这些是使用
file_field
方法从Watir访问的。不支持只点击按钮。相反,有一个set
方法,它将单击按钮,选择要上传的文件并关闭窗口
假设应用程序中的文件选择器与inkfilepicker演示页面上的文件选择器相同,则可以执行以下操作:
require 'watir-webdriver'
browser = Watir::Browser.new :firefox
# File to upload
file = 'C:UsersuserDesktopstuff.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)