【问题标题】:Code to navigate filepicker with watir?使用 watir 导航文件选择器的代码?
【发布时间】:2013-08-26 17:43:35
【问题描述】:

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

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 自动上传文件选择器?怎么样?

【问题讨论】:

  • 您应该试一试,然后就您遇到问题的特定部分提出问题。总的来说,我认为 Watir 能够工作(假设我正在查看页面的右侧部分)。
  • 我试过了,但是 watir 没有看到文件选择器上传对话框。我已经用详细信息更新了问题。
  • 我真的不知道你想自动化什么。这个“选择文件”按钮在哪里?我们需要您的实际应用程序的一些 html,或者因为它正在使用 infilepicker,所以在网站上的某个地方具有相同的功能。
  • 如果你不知道文件选择器,你为什么要回答这个问题? “选择文件”是文件选择器对话框中的一个按钮:app-fusion.com/blog/…

标签: ruby watir filepicker.io


【解决方案1】:

代码

$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)

【讨论】:

    猜你喜欢
    • 2015-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    • 1970-01-01
    • 2018-09-10
    • 1970-01-01
    • 2020-11-26
    相关资源
    最近更新 更多